Skip to content

Commit

Permalink
Merge pull request #46 from alrayyes/chore/disable-ssh-key
Browse files Browse the repository at this point in the history
Disable openssh key support temporarily
  • Loading branch information
alrayyes authored Aug 26, 2024
2 parents 59b8f0e + aca658a commit ab2b15c
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 89 deletions.
1 change: 0 additions & 1 deletion docs/resources/public_cloud_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ resource "leaseweb_public_cloud_instance" "example" {
- `market_app_id` (String) Market App ID that must be installed into the instance.**WARNING!** Changing this value once running will cause this instance to be destroyed and a new one to be created.
- `reference` (String) The identifying name set to the instance
- `root_disk_size` (Number) The root disk's size in GB. Must be at least 5 GB for Linux and FreeBSD instances and 50 GB for Windows instances
- `ssh_key` (String, Sensitive) Public SSH key to be installed into the instance. Must be used only on Linux/FreeBSD instances

### Read-Only

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,20 @@ func AdaptToCreateInstanceOpts(
)
}

if instanceResourceModel.SshKey.ValueString() != "" {
sshKey, err = value_object.NewSshKey(
instanceResourceModel.SshKey.ValueString(),
)
if err != nil {
return nil, fmt.Errorf(
"AdaptToCreateInstanceOpts: %w",
err,
)
}
}
// TODO Enable SSH key support
/**
if instanceResourceModel.SshKey.ValueString() != "" {
sshKey, err = value_object.NewSshKey(
instanceResourceModel.SshKey.ValueString(),
)
if err != nil {
return nil, fmt.Errorf(
"AdaptToCreateInstanceOpts: %w",
err,
)
}
}
*/

if instanceResourceModel.RootDiskSize.ValueInt64() != 0 {
rootDiskSize, err = value_object.NewRootDiskSize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
"github.com/stretchr/testify/assert"
)

var defaultSshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDWvBbugarDWMkELKmnzzYaxPkDpS9qDokehBM+OhgrgyTWssaREYPDHsRjq7Ldv/8kTdK9i+f9HMi/BTskZrd5npFtO2gfSgFxeUALcqNDcjpXvQJxLUShNFmtxPtQLKlreyWB1r8mcAQBC/jrWD5I+mTZ7uCs4CNV4L0eLv8J1w=="
// TODO Enable SSH key support
//var defaultSshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDWvBbugarDWMkELKmnzzYaxPkDpS9qDokehBM+OhgrgyTWssaREYPDHsRjq7Ldv/8kTdK9i+f9HMi/BTskZrd5npFtO2gfSgFxeUALcqNDcjpXvQJxLUShNFmtxPtQLKlreyWB1r8mcAQBC/jrWD5I+mTZ7uCs4CNV4L0eLv8J1w=="

func TestAdaptToCreateInstanceOpts(t *testing.T) {
t.Run("required values are set", func(t *testing.T) {
Expand All @@ -23,7 +24,6 @@ func TestAdaptToCreateInstanceOpts(t *testing.T) {
nil,
nil,
nil,
nil,
)

got, err := AdaptToCreateInstanceOpts(
Expand Down Expand Up @@ -53,7 +53,6 @@ func TestAdaptToCreateInstanceOpts(t *testing.T) {
nil,
nil,
nil,
nil,
)

got, err := AdaptToCreateInstanceOpts(
Expand All @@ -66,7 +65,8 @@ func TestAdaptToCreateInstanceOpts(t *testing.T) {
assert.Equal(t, "marketAppId", *got.MarketAppId)
assert.Equal(t, "reference", *got.Reference)
assert.Equal(t, 55, got.RootDiskSize.Value)
assert.Equal(t, defaultSshKey, got.SshKey.String())
// TODO Enable SSH key support
//assert.Equal(t, defaultSshKey, got.SshKey.String())
})

t.Run(
Expand All @@ -80,7 +80,6 @@ func TestAdaptToCreateInstanceOpts(t *testing.T) {
nil,
nil,
nil,
nil,
)

_, err := AdaptToCreateInstanceOpts(
Expand All @@ -104,7 +103,6 @@ func TestAdaptToCreateInstanceOpts(t *testing.T) {
nil,
nil,
nil,
nil,
&instanceType,
)

Expand All @@ -130,7 +128,6 @@ func TestAdaptToCreateInstanceOpts(t *testing.T) {
nil,
nil,
nil,
nil,
)

_, err := AdaptToCreateInstanceOpts(
Expand All @@ -155,7 +152,6 @@ func TestAdaptToCreateInstanceOpts(t *testing.T) {
nil,
nil,
nil,
nil,
)

_, err := AdaptToCreateInstanceOpts(
Expand All @@ -180,7 +176,6 @@ func TestAdaptToCreateInstanceOpts(t *testing.T) {
&billingFrequency,
nil,
nil,
nil,
)

_, err := AdaptToCreateInstanceOpts(
Expand All @@ -194,27 +189,30 @@ func TestAdaptToCreateInstanceOpts(t *testing.T) {
},
)

t.Run("returns error if invalid sshKey is passed", func(t *testing.T) {
sshKey := "tralala"
instance := generateInstanceModel(
nil,
nil,
nil,
nil,
&sshKey,
nil,
nil,
)

_, err := AdaptToCreateInstanceOpts(
instance,
[]string{string(publicCloud.TYPENAME_M5A_4XLARGE)},
context.TODO(),
)

assert.Error(t, err)
assert.ErrorContains(t, err, "ssh key is invalid")
})
// TODO Enable SSH key support
/**
t.Run("returns error if invalid sshKey is passed", func(t *testing.T) {
sshKey := "tralala"
instance := generateInstanceModel(
nil,
nil,
nil,
nil,
&sshKey,
nil,
nil,
)
_, err := AdaptToCreateInstanceOpts(
instance,
[]string{string(publicCloud.TYPENAME_M5A_4XLARGE)},
context.TODO(),
)
assert.Error(t, err)
assert.ErrorContains(t, err, "ssh key is invalid")
})
*/

t.Run(
"returns error if invalid rootDiskSize is passed",
Expand All @@ -225,7 +223,6 @@ func TestAdaptToCreateInstanceOpts(t *testing.T) {
nil,
nil,
nil,
nil,
&rootDiskSize,
nil,
)
Expand All @@ -251,7 +248,6 @@ func TestAdaptToCreateInstanceOpts(t *testing.T) {
nil,
nil,
nil,
nil,
&instanceType,
)

Expand All @@ -277,7 +273,6 @@ func TestAdaptToUpdateInstanceOpts(t *testing.T) {
nil,
nil,
nil,
nil,
)
instance.Id = basetypes.NewStringValue(id)

Expand All @@ -300,7 +295,6 @@ func TestAdaptToUpdateInstanceOpts(t *testing.T) {
nil,
nil,
nil,
nil,
)

got, diags := AdaptToUpdateInstanceOpts(
Expand Down Expand Up @@ -333,7 +327,6 @@ func TestAdaptToUpdateInstanceOpts(t *testing.T) {
nil,
nil,
nil,
nil,
)

_, err := AdaptToUpdateInstanceOpts(
Expand All @@ -354,7 +347,8 @@ func generateInstanceModel(
contractType *string,
contractTerm *int,
billingFrequency *int,
sshKey *string,
// TODO Enable SSH key support
//sshKey *string,
rootDiskSize *int,
instanceTypeName *string,
) model.Instance {
Expand All @@ -380,9 +374,12 @@ func generateInstanceModel(
if rootDiskSize == nil {
rootDiskSize = &defaultRootDiskSize
}
if sshKey == nil {
sshKey = &defaultSshKey
}
// TODO Enable SSH key support
/**
if sshKey == nil {
sshKey = &defaultSshKey
}
*/
if instanceTypeName == nil {
instanceTypeName = &defaultInstanceTypeName
}
Expand Down Expand Up @@ -447,7 +444,7 @@ func generateInstanceModel(
Contract: contract,
MarketAppId: basetypes.NewStringValue("marketAppId"),
Reference: basetypes.NewStringValue("reference"),
SshKey: basetypes.NewStringValue(*sshKey),
//SshKey: basetypes.NewStringValue(*sshKey),
}

return instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ func AdaptInstance(
instance.MarketAppId,
)

if instance.SshKey != nil {
plan.SshKey = basetypes.NewStringValue(instance.SshKey.String())
}
// TODO Enable SSH key support
/**
if instance.SshKey != nil {
plan.SshKey = basetypes.NewStringValue(instance.SshKey.String())
}
*/

image, err := shared.AdaptDomainEntityToResourceObject(
instance.Image,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ func TestAdaptInstance(t *testing.T) {
got.Type.As(context.TODO(), &instanceType, basetypes.ObjectAsOptions{})
assert.Equal(t, "instanceType", instanceType.Name.ValueString())

assert.Equal(t, sshKey, got.SshKey.ValueString())
// TODO Enable SSH key support
//assert.Equal(t, sshKey, got.SshKey.ValueString())
}

func Test_adaptAutoScalingGroup(t *testing.T) {
Expand Down
115 changes: 87 additions & 28 deletions internal/provider/instance_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,34 +251,37 @@ resource "leaseweb_public_cloud_instance" "test" {
})
})

t.Run("invalid sshKey", func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: providerConfig + `
resource "leaseweb_public_cloud_instance" "test" {
region = "eu-west-3"
type = {
name = "lsw.m4.4xlarge"
}
reference = "my webserver"
image = {
id = "UBUNTU_20_04_64BIT"
}
root_disk_storage_type = "CENTRAL"
contract = {
billing_frequency = 1
term = 0
type = "HOURLY"
}
ssh_key = "tralala"
}`,
ExpectError: regexp.MustCompile("Invalid Attribute Value Match"),
},
},
})
})
// TODO Enable SSH key support
/**
t.Run("invalid sshKey", func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: providerConfig + `
resource "leaseweb_public_cloud_instance" "test" {
region = "eu-west-3"
type = {
name = "lsw.m4.4xlarge"
}
reference = "my webserver"
image = {
id = "UBUNTU_20_04_64BIT"
}
root_disk_storage_type = "CENTRAL"
contract = {
billing_frequency = 1
term = 0
type = "HOURLY"
}
ssh_key = "tralala"
}`,
ExpectError: regexp.MustCompile("Invalid Attribute Value Match"),
},
},
})
})
*/

t.Run("rootDiskSize is too small", func(t *testing.T) {
resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -595,6 +598,62 @@ resource "leaseweb_public_cloud_instance" "test" {
},
)

// TODO Enable SSH key support
/**
t.Run(
"changing the sshKey is not allowed",
func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: providerConfig + `
resource "leaseweb_public_cloud_instance" "test" {
region = "eu-west-3"
type = {
name = "lsw.m3.large"
}
reference = "my webserver"
image = {
id = "UBUNTU_20_04_64BIT"
}
root_disk_storage_type = "CENTRAL"
contract = {
billing_frequency = 1
term = 0
type = "HOURLY"
}
}`,
},
{
Config: providerConfig + `
resource "leaseweb_public_cloud_instance" "test" {
region = "eu-west-3"
type = {
name = "lsw.m3.large"
}
reference = "my webserver"
image = {
id = "UBUNTU_20_04_64BIT"
}
root_disk_storage_type = "CENTRAL"
contract = {
billing_frequency = 1
term = 0
type = "HOURLY"
}
ssh_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCbRsxME8r5CbjnXcPj2IydrrDlqDjqvvK4vd4a6zDyP+Pu47HuBdbIqskdDviS/6ZHuMm7x9On/4VDRFaqVUSDAHqkJktBGgsrpoLxy5OMX2BUuxVZibW7US9hBukfi0qaBuk4P78e5ginZ+hXtZZx7li9yqs1Q27BkN+LmQ0Z6Zsbn/agq58GnuUGVwdlcilQ4WC6RoV7vtV/DIstAGDzuxA9ANrE6w6jOU25epq/OUvK7DNIm3U0PH3QK5wzYCubLuhH8tx9M7zcKJPodVPTOTsAO1RxTcwiyYTlNOg3yuubYPY+Lug1wpMPFR8WOfxSCSW9AUUTdm1Zfq7V5M99 "
}`,
ExpectError: regexp.MustCompile(
"Attribute value is not allowed to change",
),
},
},
})
},
)
*/

t.Run("changing the region triggers replacement", func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Expand Down
Loading

0 comments on commit ab2b15c

Please sign in to comment.