diff --git a/go.mod b/go.mod index fc31041ce..316b57bc8 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/nav-inc/datetime v0.1.3 github.com/outscale/osc-sdk-go/v2 v2.23.0 github.com/spf13/cast v1.6.0 + github.com/tidwall/gjson v1.18.0 ) require ( @@ -51,6 +52,8 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/oklog/run v1.1.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect diff --git a/go.sum b/go.sum index d377af8e6..d850c36fa 100644 --- a/go.sum +++ b/go.sum @@ -162,6 +162,13 @@ github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= +github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= +github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= diff --git a/outscale/config.go b/outscale/config.go index 4989bcafb..81865ce3b 100644 --- a/outscale/config.go +++ b/outscale/config.go @@ -13,14 +13,16 @@ import ( // Config ... type Config struct { - AccessKeyID string - SecretKeyID string - Region string - TokenID string - Endpoints map[string]interface{} - X509cert string - X509key string - Insecure bool + AccessKeyID string + SecretKeyID string + Region string + TokenID string + Endpoints map[string]interface{} + X509CertPath string + X509KeyPath string + Insecure bool + ConfigFilePath string + Profile string } // OutscaleClient client @@ -31,7 +33,7 @@ type OutscaleClient struct { // Client ... func (c *Config) Client() (*OutscaleClient, error) { tlsconfig := &tls.Config{InsecureSkipVerify: c.Insecure} - cert, err := tls.LoadX509KeyPair(c.X509cert, c.X509key) + cert, err := tls.LoadX509KeyPair(c.X509CertPath, c.X509KeyPath) if err == nil { tlsconfig = &tls.Config{ InsecureSkipVerify: false, diff --git a/outscale/data_source_outscale_load_balancer_vms.go b/outscale/data_source_outscale_load_balancer_vms.go index ba03d1333..2cd5c97ab 100644 --- a/outscale/data_source_outscale_load_balancer_vms.go +++ b/outscale/data_source_outscale_load_balancer_vms.go @@ -9,16 +9,14 @@ func DataSourceOutscaleLoadBalancerVms() *schema.Resource { return &schema.Resource{ Read: DataSourceOutscaleLoadBalancerVmsRead, Schema: getDataSourceSchemas(map[string]*schema.Schema{ + "filter": dataSourceFiltersSchema(), "load_balancer_name": { Type: schema.TypeString, - ForceNew: true, - Required: true, + Computed: true, }, - "backend_vm_ids": { - Type: schema.TypeList, - ForceNew: true, - Required: true, + Type: schema.TypeSet, + Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, "request_id": { diff --git a/outscale/data_source_outscale_net_test.go b/outscale/data_source_outscale_net_test.go index 711810e93..8023d3dd3 100644 --- a/outscale/data_source_outscale_net_test.go +++ b/outscale/data_source_outscale_net_test.go @@ -23,14 +23,14 @@ func TestAccNet_DataSource_basic(t *testing.T) { { Config: testAccDataSourceOutscaleVpcConfig(ipRange, tag), Check: resource.ComposeTestCheckFunc( - testAccDataSourceOutscaleVpcCheck("data.outscale_net.by_id", ipRange, tag), + testAccDataSourceOutscaleVpcCheck("data.outscale_net.by_id", ipRange), ), }, }, }) } -func testAccDataSourceOutscaleVpcCheck(name, ipRange, tag string) resource.TestCheckFunc { +func testAccDataSourceOutscaleVpcCheck(name, ipRange string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] if !ok { diff --git a/outscale/data_source_outscale_nic.go b/outscale/data_source_outscale_nic.go index f49a10025..a010c6310 100644 --- a/outscale/data_source_outscale_nic.go +++ b/outscale/data_source_outscale_nic.go @@ -244,17 +244,13 @@ func DataSourceOutscaleNicRead(d *schema.ResourceData, meta interface{}) error { return nil }) - if err != nil { - return fmt.Errorf("Error describing Network Interfaces : %s", err) - } - if err != nil { if statusCode == http.StatusNotFound { // The ENI is gone now, so just remove it from the state d.SetId("") return nil } - return fmt.Errorf("Error retrieving ENI: %s", err) + return fmt.Errorf("Error describing Network Interfaces: %s", err) } if err := utils.IsResponseEmptyOrMutiple(len(resp.GetNics()), "Nic"); err != nil { return err diff --git a/outscale/data_source_outscale_route_tables.go b/outscale/data_source_outscale_route_tables.go index 3889dae92..256b99096 100644 --- a/outscale/data_source_outscale_route_tables.go +++ b/outscale/data_source_outscale_route_tables.go @@ -184,10 +184,6 @@ func DataSourceOutscaleRouteTablesRead(d *schema.ResourceData, meta interface{}) return fmt.Errorf("[DEBUG] Error reading Internet Services (%s)", errString) } - if err != nil { - return err - } - rt := resp.GetRouteTables() if len(rt) == 0 { return fmt.Errorf("your query returned no results, please change your search criteria and try again") diff --git a/outscale/data_source_outscale_virtual_gateway_test.go b/outscale/data_source_outscale_virtual_gateway_test.go index 681e9f6d5..a17606fbf 100644 --- a/outscale/data_source_outscale_virtual_gateway_test.go +++ b/outscale/data_source_outscale_virtual_gateway_test.go @@ -4,14 +4,12 @@ import ( "fmt" "testing" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccOthers_DataSourceVirtualGateway_unattached(t *testing.T) { //t.Skip() t.Parallel() - rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ PreCheck: func() { @@ -20,7 +18,7 @@ func TestAccOthers_DataSourceVirtualGateway_unattached(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourceOutscaleVirtualGatewayUnattachedConfig(rInt), + Config: testAccDataSourceOutscaleVirtualGatewayUnattachedConfig(), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair( "data.outscale_virtual_gateway.test_by_id", "id", @@ -33,7 +31,7 @@ func TestAccOthers_DataSourceVirtualGateway_unattached(t *testing.T) { }) } -func testAccDataSourceOutscaleVirtualGatewayUnattachedConfig(rInt int) string { +func testAccDataSourceOutscaleVirtualGatewayUnattachedConfig() string { return fmt.Sprintf(` resource "outscale_virtual_gateway" "unattached" { connection_type = "ipsec.1" diff --git a/outscale/data_source_outscale_virtual_gateways_test.go b/outscale/data_source_outscale_virtual_gateways_test.go index bb78b7792..89fa0b747 100644 --- a/outscale/data_source_outscale_virtual_gateways_test.go +++ b/outscale/data_source_outscale_virtual_gateways_test.go @@ -4,15 +4,12 @@ import ( "fmt" "testing" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccOthers_DataSourceVpnGateways_unattached(t *testing.T) { //t.Skip() t.Parallel() - rInt := acctest.RandInt() - resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) @@ -20,13 +17,13 @@ func TestAccOthers_DataSourceVpnGateways_unattached(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourceOutscaleVpnGatewaysUnattachedConfig(rInt), + Config: testAccDataSourceOutscaleVpnGatewaysUnattachedConfig(), }, }, }) } -func testAccDataSourceOutscaleVpnGatewaysUnattachedConfig(rInt int) string { +func testAccDataSourceOutscaleVpnGatewaysUnattachedConfig() string { return fmt.Sprintf(` resource "outscale_virtual_gateway" "unattached" { connection_type = "ipsec.1" diff --git a/outscale/data_source_outscale_vm.go b/outscale/data_source_outscale_vm.go index ec816123f..e97bd14d4 100644 --- a/outscale/data_source_outscale_vm.go +++ b/outscale/data_source_outscale_vm.go @@ -28,7 +28,7 @@ func DataSourceOutscaleVMRead(d *schema.ResourceData, meta interface{}) error { instanceID, instanceIDOk := d.GetOk("vm_id") var err error if !filtersOk && !instanceIDOk { - return fmt.Errorf("One of filters, or instance_id must be assigned") + return fmt.Errorf("one of filters, or instance_id must be assigned") } // Build up search parameters params := oscgo.ReadVmsRequest{} diff --git a/outscale/data_source_outscale_vm_types_test.go b/outscale/data_source_outscale_vm_types_test.go index 328e8c8c5..cb8d5effe 100644 --- a/outscale/data_source_outscale_vm_types_test.go +++ b/outscale/data_source_outscale_vm_types_test.go @@ -19,7 +19,7 @@ func TestAccVM_TypesDataSource_basic(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourceOutscaleVMTypesConfig(omi, "tinav4.c1r1p1"), + Config: testAccDataSourceOutscaleVMTypesConfig(omi, "tinav5.c2r2p2"), }, }, }) diff --git a/outscale/framework_config.go b/outscale/framework_config.go index 77c13939d..fb3a7a887 100644 --- a/outscale/framework_config.go +++ b/outscale/framework_config.go @@ -4,7 +4,9 @@ import ( "context" "crypto/tls" "fmt" + "io/ioutil" "net/http" + "os" "strings" "github.com/hashicorp/terraform-plugin-framework/diag" @@ -13,6 +15,7 @@ import ( oscgo "github.com/outscale/osc-sdk-go/v2" "github.com/outscale/terraform-provider-outscale/utils" "github.com/outscale/terraform-provider-outscale/version" + "github.com/tidwall/gjson" ) // OutscaleClient client @@ -22,8 +25,14 @@ type OutscaleClient_fw struct { // Client ... func (c *frameworkProvider) Client_fw(ctx context.Context, data *ProviderModel, diags *diag.Diagnostics) (*OutscaleClient_fw, error) { + ok, err := IsProfileSet(data) + if err != nil { + return nil, err + } + if !ok { + setDefaultEnv(data) + } - setDefaultEnv(data) tlsconfig := &tls.Config{InsecureSkipVerify: c.insecure} cert, err := tls.LoadX509KeyPair(data.X509CertPath.ValueString(), data.X509KeyPath.ValueString()) if err == nil { @@ -66,6 +75,90 @@ func (c *frameworkProvider) Client_fw(ctx context.Context, data *ProviderModel, return client, nil } +func IsProfileSet(data *ProviderModel) (bool, error) { + isProfSet := false + if profileName, ok := os.LookupEnv("OSC_PROFILE"); ok || !data.Profile.IsNull() { + if data.Profile.ValueString() != "" { + profileName = data.Profile.ValueString() + } + + var profilePath string + if envPath, ok := os.LookupEnv("OSC_CONFIG_FILE"); ok || !data.ConfigFilePath.IsNull() { + if data.ConfigFilePath.ValueString() != "" { + profilePath = data.ConfigFilePath.ValueString() + } else { + profilePath = envPath + } + if profilePath == "" { + homePath, err := os.UserHomeDir() + if err != nil { + return isProfSet, err + } + profilePath = homePath + "/.osc/config.json" + } + } + jsonFile, err := ioutil.ReadFile(profilePath) + if err != nil { + return isProfSet, err + } + profile := gjson.GetBytes(jsonFile, profileName) + if !gjson.Valid(profile.String()) { + return isProfSet, fmt.Errorf("Invalid json profile file") + } + if !profile.Get("access_key").Exists() || + !profile.Get("secret_key").Exists() { + return isProfSet, fmt.Errorf("Profile 'access_key' or 'secret_key' are not defined!") + } + setProfile(data, profile) + isProfSet = true + } + return isProfSet, nil +} + +func setProfile(data *ProviderModel, profile gjson.Result) { + if data.AccessKeyId.IsNull() { + if accessKeyId := profile.Get("access_key").String(); accessKeyId != "" { + data.AccessKeyId = types.StringValue(accessKeyId) + } + } + if data.SecretKeyId.IsNull() { + if secretKeyId := profile.Get("secret_key").String(); secretKeyId != "" { + data.SecretKeyId = types.StringValue(secretKeyId) + } + } + if data.Region.IsNull() { + if profile.Get("region").Exists() { + if region := profile.Get("region").String(); region != "" { + data.Region = types.StringValue(region) + } + } + } + if data.X509CertPath.IsNull() { + if profile.Get("x509_cert_path").Exists() { + if x509Cert := profile.Get("x509_cert_path").String(); x509Cert != "" { + data.X509CertPath = types.StringValue(x509Cert) + } + } + } + if data.X509KeyPath.IsNull() { + if profile.Get("x509_key_path").Exists() { + if x509Key := profile.Get("x509_key_path").String(); x509Key != "" { + data.X509KeyPath = types.StringValue(x509Key) + } + } + } + if len(data.Endpoints) == 0 { + if profile.Get("endpoints").Exists() { + endpoints := profile.Get("endpoints").Value().(map[string]interface{}) + if endpoint := endpoints["api"].(string); endpoint != "" { + endp := make([]Endpoints, 1) + endp[0].API = types.StringValue(endpoint) + data.Endpoints = endp + } + } + } +} + func setDefaultEnv(data *ProviderModel) { if data.AccessKeyId.IsNull() { if accessKeyId := utils.GetEnvVariableValue([]string{"OSC_ACCESS_KEY", "OUTSCALE_ACCESSKEYID"}); accessKeyId != "" { @@ -96,9 +189,9 @@ func setDefaultEnv(data *ProviderModel) { } } if len(data.Endpoints) == 0 { - if endpoints := utils.GetEnvVariableValue([]string{"OSC_ENDPOINT_API", "OUTSCALE_OAPI_URL"}); endpoints != "" { + if endpoint := utils.GetEnvVariableValue([]string{"OSC_ENDPOINT_API", "OUTSCALE_OAPI_URL"}); endpoint != "" { endp := make([]Endpoints, 1) - endp[0].API = types.StringValue(endpoints) + endp[0].API = types.StringValue(endpoint) data.Endpoints = endp } } diff --git a/outscale/framework_provider.go b/outscale/framework_provider.go index 979e34846..8c2927ed8 100644 --- a/outscale/framework_provider.go +++ b/outscale/framework_provider.go @@ -22,24 +22,28 @@ func New(version string) provider.Provider { } type frameworkProvider struct { - accessKeyId types.String - secretKeyId types.String - region types.String - endpoints []Endpoints - x509CertPath string - x509KeyPath string - insecure bool - version string + accessKeyId types.String + secretKeyId types.String + region types.String + endpoints []Endpoints + x509CertPath string + x509KeyPath string + configFilePath string + insecure bool + profile string + version string } type ProviderModel struct { - AccessKeyId types.String `tfsdk:"access_key_id"` - SecretKeyId types.String `tfsdk:"secret_key_id"` - Region types.String `tfsdk:"region"` - Endpoints []Endpoints `tfsdk:"endpoints"` - X509CertPath types.String `tfsdk:"x509_cert_path"` - X509KeyPath types.String `tfsdk:"x509_key_path"` - Insecure types.Bool `tfsdk:"insecure"` + AccessKeyId types.String `tfsdk:"access_key_id"` + SecretKeyId types.String `tfsdk:"secret_key_id"` + Region types.String `tfsdk:"region"` + Endpoints []Endpoints `tfsdk:"endpoints"` + X509CertPath types.String `tfsdk:"x509_cert_path"` + X509KeyPath types.String `tfsdk:"x509_key_path"` + ConfigFilePath types.String `tfsdk:"config_file_path"` + Profile types.String `tfsdk:"profile"` + Insecure types.Bool `tfsdk:"insecure"` } type Endpoints struct { @@ -87,6 +91,14 @@ func (p *frameworkProvider) Schema(ctx context.Context, req provider.SchemaReque Optional: true, Description: "The path to your x509 key", }, + "config_file_path": schema.StringAttribute{ + Optional: true, + Description: "The path to your configuration file in which you have defined your credentials.", + }, + "profile": schema.StringAttribute{ + Optional: true, + Description: "The name of your profile in which you define your credencial", + }, "insecure": schema.BoolAttribute{ Optional: true, Description: "tls insecure connection", @@ -149,6 +161,22 @@ func (p *frameworkProvider) Configure(ctx context.Context, req provider.Configur "Either target apply the source of the value first, set the value statically in the configuration, or use the 'OSC_X509_CLIENT_KEY or OUTSCALE_X509KEY' environment variable.", ) } + if config.ConfigFilePath.IsUnknown() { + resp.Diagnostics.AddAttributeError( + path.Root("config_file_path"), + "Unknown Outscale API ConfigFilePath", + "The provider cannot create the Outscale API client as there is an unknown configuration value for the Outscale API profile. "+ + "Either target apply the source of the value first, set the value statically in the configuration, or use the 'OSC_CONFIG_FILE' environment variable.", + ) + } + if config.Profile.IsUnknown() { + resp.Diagnostics.AddAttributeError( + path.Root("profile"), + "Unknown Outscale API profile", + "The provider cannot create the Outscale API client as there is an unknown configuration value for the Outscale API profile. "+ + "Either target apply the source of the value first, set the value statically in the configuration, or use the 'OSC_PROFILE' environment variable.", + ) + } if resp.Diagnostics.HasError() { return diff --git a/outscale/provider.go b/outscale/provider.go index 25ed9a4a0..bc474ccb8 100644 --- a/outscale/provider.go +++ b/outscale/provider.go @@ -1,8 +1,13 @@ package outscale import ( + "fmt" + "io/ioutil" + "os" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/outscale/terraform-provider-outscale/utils" + "github.com/tidwall/gjson" ) var endpointServiceNames []string @@ -55,6 +60,16 @@ func Provider() *schema.Provider { Optional: true, Description: "The path to your x509 key", }, + "config_file_path": { + Type: schema.TypeString, + Optional: true, + Description: "The path to your configuration file in which you have defined your credentials.", + }, + "profile": { + Type: schema.TypeString, + Optional: true, + Description: "The name of your profile in which you define your credencial", + }, "insecure": { Type: schema.TypeBool, Optional: true, @@ -200,26 +215,115 @@ func Provider() *schema.Provider { func providerConfigureClient(d *schema.ResourceData) (interface{}, error) { config := Config{ - AccessKeyID: d.Get("access_key_id").(string), - SecretKeyID: d.Get("secret_key_id").(string), - Region: d.Get("region").(string), - Endpoints: make(map[string]interface{}), - X509cert: d.Get("x509_cert_path").(string), - X509key: d.Get("x509_key_path").(string), - Insecure: d.Get("insecure").(bool), + AccessKeyID: d.Get("access_key_id").(string), + SecretKeyID: d.Get("secret_key_id").(string), + Region: d.Get("region").(string), + Endpoints: make(map[string]interface{}), + X509CertPath: d.Get("x509_cert_path").(string), + X509KeyPath: d.Get("x509_key_path").(string), + ConfigFilePath: d.Get("config_file_path").(string), + Profile: d.Get("profile").(string), + Insecure: d.Get("insecure").(bool), } - - setProviderDefaultEnv(&config) endpointsSet := d.Get("endpoints").(*schema.Set) - for _, endpointsSetI := range endpointsSet.List() { endpoints := endpointsSetI.(map[string]interface{}) for _, endpointServiceName := range endpointServiceNames { config.Endpoints[endpointServiceName] = endpoints[endpointServiceName].(string) } } + + ok, err := IsOldProfileSet(&config) + if err != nil { + return nil, err + } + if !ok { + setProviderDefaultEnv(&config) + } return config.Client() } +func IsOldProfileSet(conf *Config) (bool, error) { + isProfSet := false + if profileName, ok := os.LookupEnv("OSC_PROFILE"); ok || conf.Profile != "" { + if conf.Profile != "" { + profileName = conf.Profile + } + + var profilePath string + if envPath, ok := os.LookupEnv("OSC_CONFIG_FILE"); ok || conf.ConfigFilePath != "" { + if conf.ConfigFilePath != "" { + profilePath = conf.ConfigFilePath + } else { + profilePath = envPath + } + if profilePath == "" { + homePath, err := os.UserHomeDir() + if err != nil { + return isProfSet, err + } + profilePath = homePath + "/.osc/config.json" + } + } + jsonFile, err := ioutil.ReadFile(profilePath) + if err != nil { + return isProfSet, err + } + profile := gjson.GetBytes(jsonFile, profileName) + if !gjson.Valid(profile.String()) { + return isProfSet, fmt.Errorf("Invalid json profile file") + } + if !profile.Get("access_key").Exists() || + !profile.Get("secret_key").Exists() { + return isProfSet, fmt.Errorf("Profile 'access_key' or 'secret_key' are not defined!") + } + setOldProfile(conf, profile) + isProfSet = true + } + return isProfSet, nil +} + +func setOldProfile(conf *Config, profile gjson.Result) { + + if conf.AccessKeyID == "" { + if accessKeyId := profile.Get("access_key").String(); accessKeyId != "" { + conf.AccessKeyID = accessKeyId + } + } + if conf.SecretKeyID == "" { + if secretKeyId := profile.Get("secret_key").String(); secretKeyId != "" { + conf.SecretKeyID = secretKeyId + } + } + if conf.Region == "" { + if profile.Get("region").Exists() { + if region := profile.Get("region").String(); region != "" { + conf.Region = region + } + } + } + if conf.X509CertPath == "" { + if profile.Get("x509_cert_path").Exists() { + if x509Cert := profile.Get("x509_cert_path").String(); x509Cert != "" { + conf.X509CertPath = x509Cert + } + } + } + if conf.X509KeyPath == "" { + if profile.Get("x509_key_path").Exists() { + if x509Key := profile.Get("x509_key_path").String(); x509Key != "" { + conf.X509KeyPath = x509Key + } + } + } + if len(conf.Endpoints) == 0 { + if profile.Get("endpoints").Exists() { + endpoints := profile.Get("endpoints").Value().(map[string]interface{}) + if endpoint := endpoints["api"].(string); endpoint != "" { + conf.Endpoints["api"] = endpoint + } + } + } +} func setProviderDefaultEnv(conf *Config) { if conf.AccessKeyID == "" { @@ -239,18 +343,17 @@ func setProviderDefaultEnv(conf *Config) { } } - if conf.X509cert == "" { + if conf.X509CertPath == "" { if x509Cert := utils.GetEnvVariableValue([]string{"OSC_X509_CLIENT_CERT", "OUTSCALE_X509CERT"}); x509Cert != "" { - conf.X509cert = x509Cert + conf.X509CertPath = x509Cert } } - if conf.X509key == "" { + if conf.X509KeyPath == "" { if x509Key := utils.GetEnvVariableValue([]string{"OSC_X509_CLIENT_KEY", "OUTSCALE_X509KEY"}); x509Key != "" { - conf.X509key = x509Key + conf.X509KeyPath = x509Key } } - if len(conf.Endpoints) == 0 { if endpoints := utils.GetEnvVariableValue([]string{"OSC_ENDPOINT_API", "OUTSCALE_OAPI_URL"}); endpoints != "" { endpointsAttributes := make(map[string]interface{}) diff --git a/outscale/resource_outscale_access_key_test.go b/outscale/resource_outscale_access_key_test.go index 8e0fe308a..77af4f01a 100644 --- a/outscale/resource_outscale_access_key_test.go +++ b/outscale/resource_outscale_access_key_test.go @@ -170,12 +170,15 @@ func testAccCheckOutscaleAccessKeyExists(resourceName string) resource.TestCheck return fmt.Errorf("No Access ID is set") } conn := testAccProvider.Meta().(*OutscaleClient).OSCAPI - - filter := oscgo.ReadSecretAccessKeyRequest{ - AccessKeyId: rs.Primary.ID, + filter := oscgo.FiltersAccessKeys{ + AccessKeyIds: &[]string{rs.Primary.ID}, + } + req := oscgo.ReadAccessKeysRequest{ + Filters: &filter, } + err := resource.Retry(2*time.Minute, func() *resource.RetryError { - _, httpResp, err := conn.AccessKeyApi.ReadSecretAccessKey(context.Background()).ReadSecretAccessKeyRequest(filter).Execute() + _, httpResp, err := conn.AccessKeyApi.ReadAccessKeys(context.Background()).ReadAccessKeysRequest(req).Execute() if err != nil { return utils.CheckThrottling(httpResp, err) } @@ -195,12 +198,15 @@ func testAccCheckOutscaleAccessKeyDestroy(s *terraform.State) error { if rs.Type != "outscale_access_key" { continue } - - filter := oscgo.ReadSecretAccessKeyRequest{ - AccessKeyId: rs.Primary.ID, + filter := oscgo.FiltersAccessKeys{ + AccessKeyIds: &[]string{rs.Primary.ID}, + } + req := oscgo.ReadAccessKeysRequest{ + Filters: &filter, } + err := resource.Retry(2*time.Minute, func() *resource.RetryError { - _, httpResp, err := conn.AccessKeyApi.ReadSecretAccessKey(context.Background()).ReadSecretAccessKeyRequest(filter).Execute() + _, httpResp, err := conn.AccessKeyApi.ReadAccessKeys(context.Background()).ReadAccessKeysRequest(req).Execute() if err != nil { return utils.CheckThrottling(httpResp, err) } diff --git a/outscale/resource_outscale_load_balancer_vms.go b/outscale/resource_outscale_load_balancer_vms.go index 919623fbd..998d68c9e 100644 --- a/outscale/resource_outscale_load_balancer_vms.go +++ b/outscale/resource_outscale_load_balancer_vms.go @@ -10,6 +10,7 @@ import ( "github.com/outscale/terraform-provider-outscale/utils" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -26,10 +27,14 @@ func ResourceLBUAttachment() *schema.Resource { ForceNew: true, Required: true, }, - "backend_vm_ids": { Type: schema.TypeSet, - Required: true, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "backend_ips": { + Type: schema.TypeSet, + Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, }, "request_id": { @@ -42,25 +47,34 @@ func ResourceLBUAttachment() *schema.Resource { func ResourceLBUAttachmentCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*OutscaleClient).OSCAPI lbuName := d.Get("load_balancer_name").(string) - vmIds := d.Get("backend_vm_ids").(*schema.Set) + vmIds := utils.SetToStringSlice(d.Get("backend_vm_ids").(*schema.Set)) + vmIps := d.Get("backend_ips").(*schema.Set) + if len(vmIds) == 0 && vmIps.Len() == 0 { + return fmt.Errorf("error: the 'backend_vm_ids' and 'backend_ips' parameters cannot both be empty") + } + if vmIps.Len() > 0 { + vm_ips, err := getVmIdsThroughVmIps(conn, vmIps) + if err != nil { + return err + } + vmIds = append(vmIds, vm_ips...) + } req := oscgo.RegisterVmsInLoadBalancerRequest{ LoadBalancerName: lbuName, - BackendVmIds: SetStringToListString(vmIds), + BackendVmIds: vmIds, } - var err error - err = resource.Retry(5*time.Minute, func() *resource.RetryError { - _, httpResp, err := conn.LoadBalancerApi. - RegisterVmsInLoadBalancer(context.Background()). - RegisterVmsInLoadBalancerRequest(req). - Execute() + + err := retry.Retry(5*time.Minute, func() *retry.RetryError { + _, httpResp, err := conn.LoadBalancerApi.RegisterVmsInLoadBalancer( + context.Background()).RegisterVmsInLoadBalancerRequest(req).Execute() if err != nil { return utils.CheckThrottling(httpResp, err) } return nil }) if err != nil { - return fmt.Errorf("Failure registering backend_vm_ids with LBU: %s", err) + return fmt.Errorf("Failure Linking LoadBalancer backend_vm_ids/backend_ips with LBU: %w", err) } d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", lbuName))) return ResourceLBUAttachmentRead(d, meta) @@ -69,121 +83,223 @@ func ResourceLBUAttachmentCreate(d *schema.ResourceData, meta interface{}) error func ResourceLBUAttachmentRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*OutscaleClient).OSCAPI lbuName := d.Get("load_balancer_name").(string) - lb, _, err := readResourceLb(conn, lbuName) + lbu, _, err := readResourceLb(conn, lbuName) if err != nil { return err } - if lb == nil { + if lbu == nil { utils.LogManuallyDeleted("LoadBalancerVms", d.Id()) d.SetId("") return nil } - - expected := d.Get("backend_vm_ids").(*schema.Set) - all_backends := schema.Set{F: expected.F} - for _, v := range *lb.BackendVmIds { - all_backends.Add(v) + if len(lbu.GetBackendVmIds()) == 0 { + utils.LogManuallyDeleted("LoadBalancerVms", d.Id()) + d.SetId("") + return nil } + expectedVmIds := d.Get("backend_vm_ids").(*schema.Set) + all_backendVms := d.Get("backend_vm_ids").(*schema.Set) + expectedIps := d.Get("backend_ips").(*schema.Set) + all_backendIps := d.Get("backend_ips").(*schema.Set) - managed := all_backends.Intersection(expected) - d.Set("backend_vm_ids", managed) + for _, vmId := range lbu.GetBackendVmIds() { + all_backendVms.Add(vmId) + } + publicIps, err := getVmIpsThroughVmIds(conn, all_backendVms) + if err != nil { + return err + } + for _, vmIp := range publicIps { + all_backendIps.Add(vmIp) + } + managedVmIds := all_backendVms.Intersection(expectedVmIds) + managedIps := all_backendIps.Intersection(expectedIps) - if managed.Len() == 0 { + if managedVmIds.Len() == 0 && managedIps.Len() == 0 { log.Printf("[WARN] not expected attachments found in LBU %s", lbuName) - log.Printf("[WARN] lbu current attachments are %#v", &all_backends) - log.Printf("[WARN] we would manage only these attachments %#v", expected) - log.Printf("[WARN] no managed attachments are present.") d.SetId("") + return nil + } + if err := d.Set("load_balancer_name", lbu.GetLoadBalancerName()); err != nil { + return err + } + if err := d.Set("backend_vm_ids", managedVmIds); err != nil { + return err + } + if err := d.Set("backend_ips", managedIps); err != nil { + return err } - return nil } func ResourceLBUAttachmentUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*OutscaleClient).OSCAPI lbuName := d.Get("load_balancer_name").(string) - var err error - - if !d.HasChange("backend_vm_ids") { - return nil + linkReq, unLinkReq, err := buildUpdateBackendsRequest(d, conn, lbuName) + if err != nil { + return err } - oldBackends, newBackends := d.GetChange("backend_vm_ids") - inter := oldBackends.(*schema.Set).Intersection(newBackends.(*schema.Set)) - created := newBackends.(*schema.Set).Difference(inter) - removed := oldBackends.(*schema.Set).Difference(inter) - - if created.Len() > 0 { - err = resource.Retry(5*time.Minute, func() *resource.RetryError { + if unLinkReq.HasBackendVmIds() { + err := retry.Retry(5*time.Minute, func() *retry.RetryError { _, httpResp, err := conn.LoadBalancerApi. - RegisterVmsInLoadBalancer(context.Background()). - RegisterVmsInLoadBalancerRequest( - oscgo.RegisterVmsInLoadBalancerRequest{ - LoadBalancerName: lbuName, - BackendVmIds: SetStringToListString(created), - }). - Execute() + UnlinkLoadBalancerBackendMachines(context.Background()). + UnlinkLoadBalancerBackendMachinesRequest(*unLinkReq).Execute() if err != nil { return utils.CheckThrottling(httpResp, err) } return nil }) if err != nil { - return fmt.Errorf("Failure registering new backend_vm_ids with LBU: %s", err) + return fmt.Errorf("failure unlinking backends from LBU: %s", err) } } - if removed.Len() > 0 { - err = resource.Retry(5*time.Minute, func() *resource.RetryError { + + if linkReq.HasBackendVmIds() { + err := retry.Retry(5*time.Minute, func() *retry.RetryError { _, httpResp, err := conn.LoadBalancerApi. - DeregisterVmsInLoadBalancer(context.Background()). - DeregisterVmsInLoadBalancerRequest( - oscgo.DeregisterVmsInLoadBalancerRequest{ - LoadBalancerName: lbuName, - BackendVmIds: SetStringToListString(removed), - }). - Execute() + LinkLoadBalancerBackendMachines(context.Background()). + LinkLoadBalancerBackendMachinesRequest(*linkReq).Execute() if err != nil { return utils.CheckThrottling(httpResp, err) } return nil }) if err != nil { - return fmt.Errorf("Failure deregistering old backend_vm_ids from LBU: %s", err) + return fmt.Errorf("failure linking backends to LBU: %s", err) } } return ResourceLBUAttachmentRead(d, meta) } +func buildUpdateBackendsRequest(d *schema.ResourceData, conn *oscgo.APIClient, lbuName string) (*oscgo.LinkLoadBalancerBackendMachinesRequest, *oscgo.UnlinkLoadBalancerBackendMachinesRequest, error) { + linkReq := oscgo.NewLinkLoadBalancerBackendMachinesRequest(lbuName) + unLinkReq := oscgo.NewUnlinkLoadBalancerBackendMachinesRequest(lbuName) + linkVmIds := make([]string, 0, 0) + unlinkVmIds := make([]string, 0, 0) + if d.HasChange("backend_vm_ids") { + oldBackends, newBackends := d.GetChange("backend_vm_ids") + inter := oldBackends.(*schema.Set).Intersection(newBackends.(*schema.Set)) + created := newBackends.(*schema.Set).Difference(inter) + removed := oldBackends.(*schema.Set).Difference(inter) + + if created.Len() > 0 { + linkVmIds = append(linkVmIds, utils.SetToStringSlice(created)...) + } + if removed.Len() > 0 { + unlinkVmIds = append(linkVmIds, utils.SetToStringSlice(removed)...) + } + } + if d.HasChange("backend_ips") { + oldBackends, newBackends := d.GetChange("backend_ips") + inter := oldBackends.(*schema.Set).Intersection(newBackends.(*schema.Set)) + created := newBackends.(*schema.Set).Difference(inter) + removed := oldBackends.(*schema.Set).Difference(inter) + + if created.Len() > 0 { + vmIds, err := getVmIdsThroughVmIps(conn, created) + if err != nil { + return nil, nil, err + } + linkVmIds = append(linkVmIds, vmIds...) + } + if removed.Len() > 0 { + vmIds, err := getVmIdsThroughVmIps(conn, removed) + if err != nil { + return nil, nil, err + } + unlinkVmIds = append(linkVmIds, vmIds...) + } + } + if len(linkVmIds) > 0 { + linkReq.SetBackendVmIds(linkVmIds) + } + if len(unlinkVmIds) > 0 { + unLinkReq.SetBackendVmIds(unlinkVmIds) + } + return linkReq, unLinkReq, nil +} + func ResourceLBUAttachmentDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*OutscaleClient).OSCAPI lbuName := d.Get("load_balancer_name").(string) - vmIds := d.Get("backend_vm_ids").(*schema.Set) - - req := oscgo.DeregisterVmsInLoadBalancerRequest{ - LoadBalancerName: lbuName, - BackendVmIds: SetStringToListString(vmIds), + unlinkVmIds := utils.SetToStringSlicePtr(d.Get("backend_vm_ids").(*schema.Set)) + if ips := d.Get("backend_ips").(*schema.Set); ips.Len() > 0 { + vmIps, err := getVmIdsThroughVmIps(conn, ips) + if err != nil { + return err + } + *unlinkVmIds = append(*unlinkVmIds, vmIps...) } - var err error - err = resource.Retry(5*time.Minute, func() *resource.RetryError { + err := retry.Retry(5*time.Minute, func() *retry.RetryError { _, httpResp, err := conn.LoadBalancerApi. - DeregisterVmsInLoadBalancer(context.Background()). - DeregisterVmsInLoadBalancerRequest(req). - Execute() + UnlinkLoadBalancerBackendMachines(context.Background()). + UnlinkLoadBalancerBackendMachinesRequest( + oscgo.UnlinkLoadBalancerBackendMachinesRequest{ + LoadBalancerName: lbuName, + BackendVmIds: unlinkVmIds, + }).Execute() if err != nil { return utils.CheckThrottling(httpResp, err) } return nil }) if err != nil { - return fmt.Errorf("Failure deregistering backend_vm_ids from LBU: %s", err) + return fmt.Errorf("failure unlinking backend_ips from LBU: %s", err) } return nil } -func SetStringToListString(set *schema.Set) []string { - result := make([]string, 0, set.Len()) - for _, val := range set.List() { - result = append(result, val.(string)) +func getVmIdsThroughVmIps(conn *oscgo.APIClient, vmIps *schema.Set) ([]string, error) { + filterIps := oscgo.NewFiltersVm() + filterIps.SetPublicIps(utils.SetToStringSlice(vmIps)) + var resp oscgo.ReadVmsResponse + err := retry.Retry(30*time.Second, func() *retry.RetryError { + rp, httpResp, err := conn.VmApi.ReadVms(context.Background()).ReadVmsRequest(oscgo.ReadVmsRequest{ + Filters: filterIps}).Execute() + if err != nil { + return utils.CheckThrottling(httpResp, err) + } + resp = rp + return nil + }) + if err != nil { + return nil, err + } + vms := resp.GetVms() + if len(vms) == 0 { + return nil, fmt.Errorf("not found Vms with public_ip [%v]", utils.SetToStringSlice(vmIps)) + } + vmsIds := make([]string, 0, len(vms)) + for _, vm := range vms { + vmsIds = append(vmsIds, vm.GetVmId()) + } + return vmsIds, nil +} + +func getVmIpsThroughVmIds(conn *oscgo.APIClient, vmIds *schema.Set) ([]string, error) { + filters := oscgo.NewFiltersVm() + filters.SetVmIds(utils.SetToStringSlice(vmIds)) + var resp oscgo.ReadVmsResponse + err := retry.Retry(30*time.Second, func() *retry.RetryError { + rp, httpResp, err := conn.VmApi.ReadVms(context.Background()).ReadVmsRequest(oscgo.ReadVmsRequest{ + Filters: filters}).Execute() + if err != nil { + return utils.CheckThrottling(httpResp, err) + } + resp = rp + return nil + }) + if err != nil { + return nil, err + } + vms := resp.GetVms() + if len(vms) == 0 { + return nil, fmt.Errorf("not found Vms with vm_ids [%v]", utils.SetToStringSlice(vmIds)) + } + publicIps := make([]string, 0, len(vms)) + for _, vm := range vms { + publicIps = append(publicIps, vm.GetPublicIp()) } - return result + return publicIps, nil } diff --git a/outscale/resource_outscale_load_balancer_vms_test.go b/outscale/resource_outscale_load_balancer_vms_test.go index 7116493a2..2c9d24829 100644 --- a/outscale/resource_outscale_load_balancer_vms_test.go +++ b/outscale/resource_outscale_load_balancer_vms_test.go @@ -18,6 +18,7 @@ func TestAccVM_WithLBUAttachment_basic(t *testing.T) { var conf oscgo.LoadBalancer omi := os.Getenv("OUTSCALE_IMAGEID") rand := acctest.RandIntRange(0, 50) + region := utils.GetRegion() testCheckInstanceAttached := func(count int) resource.TestCheckFunc { return func(*terraform.State) error { if conf.BackendVmIds != nil { @@ -32,14 +33,27 @@ func TestAccVM_WithLBUAttachment_basic(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheckValues(t) }, - IDRefreshName: "outscale_load_balancer.barTach", + IDRefreshName: "outscale_load_balancer.lbu_test", Providers: testAccProviders, - CheckDestroy: testAccCheckOutscaleLBUDestroy, Steps: []resource.TestStep{ { - Config: testAccOutscaleLBUAttachmentConfig1(rand, omi, utils.GetRegion()), + Config: testAccOutscaleLBUAttachmentConfig1(rand, omi, region), Check: resource.ComposeTestCheckFunc( - testAccCheckOutscaleLBUExists("outscale_load_balancer.barTach", &conf), + testAccCheckOutscaleLBUExists("outscale_load_balancer.lbu_test", &conf), + testCheckInstanceAttached(1), + ), + }, + { + Config: testAcc_ConfigLBUAttachmentAddUpdate(omi, region), + Check: resource.ComposeTestCheckFunc( + testAccCheckOutscaleLBUExists("outscale_load_balancer.lbu_test", &conf), + testCheckInstanceAttached(2), + ), + }, + { + Config: testAcc_ConfigLBUAttachmentRemoveUpdate(omi, region), + Check: resource.ComposeTestCheckFunc( + testAccCheckOutscaleLBUExists("outscale_load_balancer.lbu_test", &conf), testCheckInstanceAttached(1), ), }, @@ -50,7 +64,7 @@ func TestAccVM_WithLBUAttachment_basic(t *testing.T) { // add one attachment func testAccOutscaleLBUAttachmentConfig1(num int, omi, region string) string { return fmt.Sprintf(` -resource "outscale_load_balancer" "barTach" { +resource "outscale_load_balancer" "lbu_test" { load_balancer_name = "load-test-%d" subregion_names = ["%[2]sa"] listeners { @@ -72,35 +86,26 @@ resource "outscale_security_group" "sg_lb1" { resource "outscale_vm" "foo1" { image_id = "%[3]s" - vm_type = "tinav4.c1r1p1" + vm_type = "tinav5.c2r2p2" security_group_ids = [outscale_security_group.sg_lb1.security_group_id] } -resource "outscale_security_group" "sg_lb02" { - security_group_name = "terraform_test_lb02" - description = "Used in the terraform acceptance tests" - tags { - key = "Name" - value = "tf-acc-test" - } -} - resource "outscale_vm" "foo2" { image_id = "%[3]s" - vm_type = "tinav4.c1r1p1" - security_group_ids = [outscale_security_group.sg_lb02.security_group_id] + vm_type = "tinav5.c2r2p2" + security_group_ids = [outscale_security_group.sg_lb1.security_group_id] } resource "outscale_load_balancer_vms" "foo1" { - load_balancer_name = outscale_load_balancer.barTach.id - backend_vm_ids = [outscale_vm.foo1.id] + load_balancer_name = outscale_load_balancer.lbu_test.load_balancer_name + backend_vm_ids = [outscale_vm.foo1.vm_id] } `, num, region, omi) } func testAcc_ConfigLBUAttachmentAddUpdate(omi, region string) string { return fmt.Sprintf(` -resource "outscale_load_balancer" "barTach" { +resource "outscale_load_balancer" "lbu_test" { load_balancer_name = "load-test12" subregion_names = ["%[1]sa"] listeners { @@ -122,35 +127,27 @@ resource "outscale_security_group" "sg_lb1" { resource "outscale_vm" "foo1" { image_id = "%[2]s" - vm_type = "tinav4.c1r1p1" + vm_type = "tinav5.c2r2p2" security_group_ids = [outscale_security_group.sg_lb1.security_group_id] } -resource "outscale_security_group" "sg_lb02" { - security_group_name = "terraform_test_lb02" - description = "Used in the terraform acceptance tests" - tags { - key = "Name" - value = "tf-acc-test" - } -} - resource "outscale_vm" "foo2" { image_id = "%[2]s" - vm_type = "tinav4.c1r1p1" - security_group_ids = [outscale_security_group.sg_lb02.security_group_id] + vm_type = "tinav5.c2r2p2" + security_group_ids = [outscale_security_group.sg_lb1.security_group_id] } resource "outscale_load_balancer_vms" "foo1" { - load_balancer_name = outscale_load_balancer.barTach.id - backend_vm_ids = [outscale_vm.foo1.id, outscale_vm.foo2.id] + load_balancer_name = outscale_load_balancer.lbu_test.load_balancer_name + backend_vm_ids = [outscale_vm.foo1.vm_id] + backend_ips = [outscale_vm.foo2.public_ip] } `, region, omi) } func testAcc_ConfigLBUAttachmentRemoveUpdate(omi, region string) string { return fmt.Sprintf(` -resource "outscale_load_balancer" "barTach" { +resource "outscale_load_balancer" "lbu_test" { load_balancer_name = "load-test12" subregion_names = ["%sa"] listeners { @@ -172,28 +169,19 @@ resource "outscale_security_group" "sg_lb1" { resource "outscale_vm" "foo1" { image_id = "%[2]s" - vm_type = "tinav4.c1r1p1" + vm_type = "tinav5.c2r2p2" security_group_ids = [outscale_security_group.sg_lb1.security_group_id] } -resource "outscale_security_group" "sg_lb02" { - security_group_name = "terraform_test_lb02" - description = "Used in the terraform acceptance tests" - tags { - key = "Name" - value = "tf-acc-test" - } -} - resource "outscale_vm" "foo2" { image_id = "%[2]s" - vm_type = "tinav4.c1r1p1" - security_group_ids = [outscale_security_group.sg_lb02.security_group_id] + vm_type = "tinav5.c2r2p2" + security_group_ids = [outscale_security_group.sg_lb1.security_group_id] } resource "outscale_load_balancer_vms" "foo1" { - load_balancer_name = outscale_load_balancer.barTach.id - backend_vm_ids = [outscale_vm.foo2.id] + load_balancer_name = outscale_load_balancer.lbu_test.load_balancer_name + backend_vm_ids = [outscale_vm.foo1.vm_id] } `, region, omi) } diff --git a/outscale/resource_outscale_route_table_link.go b/outscale/resource_outscale_route_table_link.go index 31ea79428..a7f334e27 100644 --- a/outscale/resource_outscale_route_table_link.go +++ b/outscale/resource_outscale_route_table_link.go @@ -71,10 +71,6 @@ func ResourceOutscaleLinkRouteTableCreate(d *schema.ResourceData, meta interface resp = rp return nil }) - if err != nil { - return err - } - // Set the ID and return var errString string if err != nil { diff --git a/outscale/resource_outscale_security_group_test.go b/outscale/resource_outscale_security_group_test.go index 738505d5f..f6d52cc0b 100644 --- a/outscale/resource_outscale_security_group_test.go +++ b/outscale/resource_outscale_security_group_test.go @@ -4,15 +4,12 @@ import ( "fmt" "testing" - oscgo "github.com/outscale/osc-sdk-go/v2" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func TestAccNet_WithSecurityGroup(t *testing.T) { - var group oscgo.SecurityGroup rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ @@ -23,7 +20,7 @@ func TestAccNet_WithSecurityGroup(t *testing.T) { { Config: testAccOutscaleSecurityGroupConfig(rInt), Check: resource.ComposeTestCheckFunc( - testAccCheckOutscaleSecurityGroupRuleExists("outscale_security_group.web", &group), + testAccCheckOutscaleSecurityGroupRuleExists("outscale_security_group.web"), resource.TestCheckResourceAttr( "outscale_security_group.web", "security_group_name", fmt.Sprintf("terraform_test_%d", rInt)), ), @@ -48,7 +45,7 @@ func testAccCheckOutscaleSGRuleDestroy(s *terraform.State) error { return nil } -func testAccCheckOutscaleSecurityGroupRuleExists(n string, group *oscgo.SecurityGroup) resource.TestCheckFunc { +func testAccCheckOutscaleSecurityGroupRuleExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { conn := testAccProvider.Meta().(*OutscaleClient).OSCAPI diff --git a/outscale/resource_outscale_virtual_gateway_route_propagation_test.go b/outscale/resource_outscale_virtual_gateway_route_propagation_test.go index bf3a06869..2abc83bc1 100644 --- a/outscale/resource_outscale_virtual_gateway_route_propagation_test.go +++ b/outscale/resource_outscale_virtual_gateway_route_propagation_test.go @@ -15,7 +15,6 @@ import ( func TestAccNet_WithVirtualRoutePropagation_basic(t *testing.T) { t.Parallel() - rBgpAsn := utils.RandIntRange(64512, 65534) resource.Test(t, resource.TestCase{ PreCheck: func() { @@ -25,7 +24,7 @@ func TestAccNet_WithVirtualRoutePropagation_basic(t *testing.T) { CheckDestroy: testAccCheckOAPIVirtualRoutePropagationDestroy, Steps: []resource.TestStep{ { - Config: testAccOutscaleVpnRoutePropagationConfig(rBgpAsn), + Config: testAccOutscaleVpnRoutePropagationConfig(), Check: resource.ComposeTestCheckFunc( testAccOutscaleVpnRoutePropagation( "outscale_virtual_gateway_route_propagation.outscale_virtual_gateway_route_propagation", @@ -97,7 +96,7 @@ func testAccOutscaleVpnRoutePropagation(routeProp string) resource.TestCheckFunc } } -func testAccOutscaleVpnRoutePropagationConfig(rBgpAsn int) string { +func testAccOutscaleVpnRoutePropagationConfig() string { return fmt.Sprintf(` resource "outscale_virtual_gateway" "outscale_virtual_gateway" { connection_type = "ipsec.1" diff --git a/outscale/resource_outscale_vm.go b/outscale/resource_outscale_vm.go index ea253bfcc..7814a7de4 100644 --- a/outscale/resource_outscale_vm.go +++ b/outscale/resource_outscale_vm.go @@ -717,7 +717,7 @@ func ResourceOutscaleVM() *schema.Resource { func resourceOAPIVMCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*OutscaleClient).OSCAPI - vmOpts, bsuMapsTags, err := buildCreateVmsRequest(d, meta) + vmOpts, bsuMapsTags, err := buildCreateVmsRequest(d) if err != nil { return err } @@ -1164,7 +1164,7 @@ func resourceOAPIVMDelete(d *schema.ResourceData, meta interface{}) error { return nil } -func buildCreateVmsRequest(d *schema.ResourceData, meta interface{}) (oscgo.CreateVmsRequest, []map[string]interface{}, error) { +func buildCreateVmsRequest(d *schema.ResourceData) (oscgo.CreateVmsRequest, []map[string]interface{}, error) { request := oscgo.CreateVmsRequest{ DeletionProtection: oscgo.PtrBool(d.Get("deletion_protection").(bool)), BootOnCreation: oscgo.PtrBool(true), diff --git a/outscale/resource_outscale_volume_link_test.go b/outscale/resource_outscale_volume_link_test.go index fa6d9ede3..c62ae94dc 100644 --- a/outscale/resource_outscale_volume_link_test.go +++ b/outscale/resource_outscale_volume_link_test.go @@ -18,7 +18,6 @@ func TestAccVM_WithVolumeAttachment_basic(t *testing.T) { keypair := os.Getenv("OUTSCALE_KEYPAIR") var i oscgo.Vm - var v oscgo.Volume resource.Test(t, resource.TestCase{ PreCheck: func() { @@ -35,7 +34,7 @@ func TestAccVM_WithVolumeAttachment_basic(t *testing.T) { "outscale_volume_link.ebs_att", "device_name", "/dev/sdh"), testAccCheckOutscaleVMExists("outscale_vm.web", &i), testAccCheckOAPIVolumeAttachmentExists( - "outscale_volume_link.ebs_att", &i, &v), + "outscale_volume_link.ebs_att", &i), ), }, }, @@ -86,7 +85,7 @@ func testAccCheckOAPIVolumeAttachmentDestroy(s *terraform.State) error { return nil } -func testAccCheckOAPIVolumeAttachmentExists(n string, i *oscgo.Vm, v *oscgo.Volume) resource.TestCheckFunc { +func testAccCheckOAPIVolumeAttachmentExists(n string, i *oscgo.Vm) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { diff --git a/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-87_load_balancer_listener_rule_datasource_attributes_ok/step1.load_balancer_listener_rule_datasource_attributes_ok.ref b/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-87_load_balancer_listener_rule_datasource_attributes_ok/step1.load_balancer_listener_rule_datasource_attributes_ok.ref index 5b301fc06..4f16cd6a9 100644 --- a/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-87_load_balancer_listener_rule_datasource_attributes_ok/step1.load_balancer_listener_rule_datasource_attributes_ok.ref +++ b/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-87_load_balancer_listener_rule_datasource_attributes_ok/step1.load_balancer_listener_rule_datasource_attributes_ok.ref @@ -221,6 +221,7 @@ { "schema_version": 0, "attributes": { + "backend_ips": null, "backend_vm_ids": [ "##id-3##" ], diff --git a/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-88_load_balancer_listener_rule_resource_attributes_ok/step1.load_balancer_listener_rule_resource_attributes_ok.ref b/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-88_load_balancer_listener_rule_resource_attributes_ok/step1.load_balancer_listener_rule_resource_attributes_ok.ref index e38598a83..a576438ab 100644 --- a/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-88_load_balancer_listener_rule_resource_attributes_ok/step1.load_balancer_listener_rule_resource_attributes_ok.ref +++ b/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-88_load_balancer_listener_rule_resource_attributes_ok/step1.load_balancer_listener_rule_resource_attributes_ok.ref @@ -237,6 +237,7 @@ { "schema_version": 0, "attributes": { + "backend_ips": null, "backend_vm_ids": [ "##id-6##" ], diff --git a/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-88_load_balancer_listener_rule_resource_attributes_ok/step2.load_balancer_listener_rule_resource_attributes_ok.ref b/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-88_load_balancer_listener_rule_resource_attributes_ok/step2.load_balancer_listener_rule_resource_attributes_ok.ref index fdbfb5186..f2f61d073 100644 --- a/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-88_load_balancer_listener_rule_resource_attributes_ok/step2.load_balancer_listener_rule_resource_attributes_ok.ref +++ b/tests/qa_provider_oapi/data/load_balancer_listener_rule/TF-88_load_balancer_listener_rule_resource_attributes_ok/step2.load_balancer_listener_rule_resource_attributes_ok.ref @@ -239,6 +239,7 @@ { "schema_version": 0, "attributes": { + "backend_ips": [], "backend_vm_ids": [ "##id-1##" ], diff --git a/tests/qa_provider_oapi/data/load_balancer_listener_rules/TF-89_load_balancer_listener_rules_datasource_attributes_ok/step1.load_balancer_listener_rules_datasource_attributes_ok.ref b/tests/qa_provider_oapi/data/load_balancer_listener_rules/TF-89_load_balancer_listener_rules_datasource_attributes_ok/step1.load_balancer_listener_rules_datasource_attributes_ok.ref index 6e1455378..7f81ca54d 100644 --- a/tests/qa_provider_oapi/data/load_balancer_listener_rules/TF-89_load_balancer_listener_rules_datasource_attributes_ok/step1.load_balancer_listener_rules_datasource_attributes_ok.ref +++ b/tests/qa_provider_oapi/data/load_balancer_listener_rules/TF-89_load_balancer_listener_rules_datasource_attributes_ok/step1.load_balancer_listener_rules_datasource_attributes_ok.ref @@ -19,24 +19,24 @@ "listener_rules": [ { "action": "forward", - "host_name_pattern": "", + "host_name_pattern": "*.abc.-.abc.*.com", "listener_id": "##id-1##", "listener_rule_id": "##id-2##", "listener_rule_name": "########", - "path_pattern": "*.abc.*.abc.*.com", - "priority": 10, + "path_pattern": "", + "priority": 1, "vm_ids": [ "##id-3##" ] }, { "action": "forward", - "host_name_pattern": "*.abc.-.abc.*.com", + "host_name_pattern": "", "listener_id": "##id-1##", "listener_rule_id": "##id-4##", "listener_rule_name": "########", - "path_pattern": "", - "priority": 1, + "path_pattern": "*.abc.*.abc.*.com", + "priority": 10, "vm_ids": [ "##id-3##" ] @@ -163,7 +163,7 @@ "action": "forward", "host_name_pattern": "", "listener_id": "##id-1##", - "listener_rule_id": "##id-2##", + "listener_rule_id": "##id-4##", "listener_rule_name": "########", "path_pattern": "*.abc.*.abc.*.com", "priority": 10 @@ -206,7 +206,7 @@ "action": "forward", "host_name_pattern": "*.abc.-.abc.*.com", "listener_id": "##id-1##", - "listener_rule_id": "##id-4##", + "listener_rule_id": "##id-2##", "listener_rule_name": "########", "path_pattern": "", "priority": 1 @@ -237,6 +237,7 @@ { "schema_version": 0, "attributes": { + "backend_ips": null, "backend_vm_ids": [ "##id-3##" ], diff --git a/tests/qa_provider_oapi/data/load_balancer_vm_health/TF-92_load_balancer_vm_health_datasource_attributes_ok/step1.load_balancer_vm_health_datasource_attributes_ok.ref b/tests/qa_provider_oapi/data/load_balancer_vm_health/TF-92_load_balancer_vm_health_datasource_attributes_ok/step1.load_balancer_vm_health_datasource_attributes_ok.ref index 556b9a0bd..3190caae9 100644 --- a/tests/qa_provider_oapi/data/load_balancer_vm_health/TF-92_load_balancer_vm_health_datasource_attributes_ok/step1.load_balancer_vm_health_datasource_attributes_ok.ref +++ b/tests/qa_provider_oapi/data/load_balancer_vm_health/TF-92_load_balancer_vm_health_datasource_attributes_ok/step1.load_balancer_vm_health_datasource_attributes_ok.ref @@ -212,6 +212,7 @@ { "schema_version": 0, "attributes": { + "backend_ips": null, "backend_vm_ids": [ "##id-0##" ], diff --git a/tests/qa_provider_oapi/data/load_balancer_vms/TF-93_load_balancer_vms_resource_attributes_ok/step1.load_balancer_vms_resource_attributes_ok.ref b/tests/qa_provider_oapi/data/load_balancer_vms/TF-93_load_balancer_vms_resource_attributes_ok/step1.load_balancer_vms_resource_attributes_ok.ref index f9b1a68ae..0a8128aee 100644 --- a/tests/qa_provider_oapi/data/load_balancer_vms/TF-93_load_balancer_vms_resource_attributes_ok/step1.load_balancer_vms_resource_attributes_ok.ref +++ b/tests/qa_provider_oapi/data/load_balancer_vms/TF-93_load_balancer_vms_resource_attributes_ok/step1.load_balancer_vms_resource_attributes_ok.ref @@ -116,6 +116,7 @@ { "schema_version": 0, "attributes": { + "backend_ips": null, "backend_vm_ids": [ "##id-3##", "##id-4##" diff --git a/tests/qa_provider_oapi/data/load_balancer_vms/TF-93_load_balancer_vms_resource_attributes_ok/step2.load_balancer_vms_resource_remove_vm_ok.ref b/tests/qa_provider_oapi/data/load_balancer_vms/TF-93_load_balancer_vms_resource_attributes_ok/step2.load_balancer_vms_resource_remove_vm_ok.ref index b7a5a8578..c29ddcdc1 100644 --- a/tests/qa_provider_oapi/data/load_balancer_vms/TF-93_load_balancer_vms_resource_attributes_ok/step2.load_balancer_vms_resource_remove_vm_ok.ref +++ b/tests/qa_provider_oapi/data/load_balancer_vms/TF-93_load_balancer_vms_resource_attributes_ok/step2.load_balancer_vms_resource_remove_vm_ok.ref @@ -119,8 +119,9 @@ { "schema_version": 0, "attributes": { + "backend_ips": [], "backend_vm_ids": [ - "##id-2##" + "##id-1##" ], "id": "##id-5##", "load_balancer_name": "########", @@ -197,7 +198,7 @@ "deletion_protection": false, "get_admin_password": null, "hypervisor": "xen", - "id": "##id-2##", + "id": "##id-1##", "image_id": "##id-9##", "is_source_dest_checked": false, "keypair_name": "KP-TF93", @@ -243,7 +244,7 @@ ], "timeouts": null, "user_data": "ICAgICAjIS9iaW4vYmFzaAogICAgcHVzaGQgL2hvbWUKICAgIG5vaHVwIHB5dGhvbiAtbSBTaW1wbGVIVFRQU2VydmVyIDgwODAK", - "vm_id": "##id-2##", + "vm_id": "##id-1##", "vm_initiated_shutdown_behavior": "stop", "vm_type": "###vm_type###" }, @@ -281,7 +282,7 @@ "deletion_protection": false, "get_admin_password": null, "hypervisor": "xen", - "id": "##id-1##", + "id": "##id-2##", "image_id": "##id-9##", "is_source_dest_checked": false, "keypair_name": "KP-TF93", @@ -327,7 +328,7 @@ ], "timeouts": null, "user_data": "ICAgICAjIS9iaW4vYmFzaAogICAgcHVzaGQgL2hvbWUKICAgIG5vaHVwIHB5dGhvbiAtbSBTaW1wbGVIVFRQU2VydmVyIDgwODAK", - "vm_id": "##id-1##", + "vm_id": "##id-2##", "vm_initiated_shutdown_behavior": "stop", "vm_type": "###vm_type###" }, diff --git a/tests/qa_provider_oapi/data/nets/TF-113_nics_datasource_attributes_ok/step1.nics_datasource_attributes_ok.ref b/tests/qa_provider_oapi/data/nets/TF-113_nics_datasource_attributes_ok/step1.nics_datasource_attributes_ok.ref index 8fcf630f4..6c204e02a 100644 --- a/tests/qa_provider_oapi/data/nets/TF-113_nics_datasource_attributes_ok/step1.nics_datasource_attributes_ok.ref +++ b/tests/qa_provider_oapi/data/nets/TF-113_nics_datasource_attributes_ok/step1.nics_datasource_attributes_ok.ref @@ -150,11 +150,20 @@ "account_id": "##id-1##", "description": "", "is_source_dest_checked": true, - "link_nic": [], + "link_nic": [ + { + "delete_on_vm_deletion": false, + "device_number": 2, + "link_nic_id": "##id-12##", + "state": "attached", + "vm_account_id": "##id-1##", + "vm_id": "##id-3##" + } + ], "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-12##", + "nic_id": "##id-13##", "private_dns_name": "########", "private_ips": [ { @@ -162,6 +171,12 @@ "link_public_ip": [], "private_dns_name": "########", "private_ip": "########" + }, + { + "is_primary": false, + "link_public_ip": [], + "private_dns_name": "########", + "private_ip": "########" } ], "security_groups": [ @@ -170,17 +185,13 @@ "security_group_name": "Terraform-SG-TF113" } ], - "state": "available", - "subnet_id": "##id-13##", - "subregion_name": "us-east-2b", + "state": "in-use", + "subnet_id": "##id-7##", + "subregion_name": "us-east-2a", "tags": [ { - "key": "Key:", - "value": ":value-tags" - }, - { - "key": "Key-2", - "value": "value-tags-2" + "key": "Name", + "value": "Nic-2" } ] }, @@ -188,20 +199,11 @@ "account_id": "##id-1##", "description": "", "is_source_dest_checked": true, - "link_nic": [ - { - "delete_on_vm_deletion": false, - "device_number": 2, - "link_nic_id": "##id-14##", - "state": "attached", - "vm_account_id": "##id-1##", - "vm_id": "##id-3##" - } - ], + "link_nic": [], "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-15##", + "nic_id": "##id-14##", "private_dns_name": "########", "private_ips": [ { @@ -209,12 +211,6 @@ "link_public_ip": [], "private_dns_name": "########", "private_ip": "########" - }, - { - "is_primary": false, - "link_public_ip": [], - "private_dns_name": "########", - "private_ip": "########" } ], "security_groups": [ @@ -223,13 +219,17 @@ "security_group_name": "Terraform-SG-TF113" } ], - "state": "in-use", - "subnet_id": "##id-7##", - "subregion_name": "us-east-2a", + "state": "available", + "subnet_id": "##id-15##", + "subregion_name": "us-east-2b", "tags": [ { - "key": "Name", - "value": "Nic-2" + "key": "Key:", + "value": ":value-tags" + }, + { + "key": "Key-2", + "value": "value-tags-2" } ] } @@ -256,11 +256,20 @@ "account_id": "##id-1##", "description": "", "is_source_dest_checked": true, - "link_nic": [], + "link_nic": [ + { + "delete_on_vm_deletion": false, + "device_number": 2, + "link_nic_id": "##id-12##", + "state": "attached", + "vm_account_id": "##id-1##", + "vm_id": "##id-3##" + } + ], "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-12##", + "nic_id": "##id-13##", "private_dns_name": "########", "private_ips": [ { @@ -268,6 +277,12 @@ "link_public_ip": [], "private_dns_name": "########", "private_ip": "########" + }, + { + "is_primary": false, + "link_public_ip": [], + "private_dns_name": "########", + "private_ip": "########" } ], "security_groups": [ @@ -276,38 +291,25 @@ "security_group_name": "Terraform-SG-TF113" } ], - "state": "available", - "subnet_id": "##id-13##", - "subregion_name": "us-east-2b", + "state": "in-use", + "subnet_id": "##id-7##", + "subregion_name": "us-east-2a", "tags": [ { - "key": "Key:", - "value": ":value-tags" - }, - { - "key": "Key-2", - "value": "value-tags-2" + "key": "Name", + "value": "Nic-2" } ] }, { "account_id": "##id-1##", - "description": "Primary network interface", + "description": "", "is_source_dest_checked": true, - "link_nic": [ - { - "delete_on_vm_deletion": true, - "device_number": 0, - "link_nic_id": "##id-9##", - "state": "attached", - "vm_account_id": "##id-1##", - "vm_id": "##id-3##" - } - ], + "link_nic": [], "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-10##", + "nic_id": "##id-14##", "private_dns_name": "########", "private_ips": [ { @@ -323,20 +325,29 @@ "security_group_name": "Terraform-SG-TF113" } ], - "state": "in-use", - "subnet_id": "##id-7##", - "subregion_name": "us-east-2a", - "tags": [] + "state": "available", + "subnet_id": "##id-15##", + "subregion_name": "us-east-2b", + "tags": [ + { + "key": "Key-2", + "value": "value-tags-2" + }, + { + "key": "Key:", + "value": ":value-tags" + } + ] }, { "account_id": "##id-1##", - "description": "", + "description": "TF-113", "is_source_dest_checked": true, "link_nic": [ { "delete_on_vm_deletion": false, - "device_number": 2, - "link_nic_id": "##id-14##", + "device_number": 1, + "link_nic_id": "##id-2##", "state": "attached", "vm_account_id": "##id-1##", "vm_id": "##id-3##" @@ -345,7 +356,7 @@ "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-15##", + "nic_id": "##id-5##", "private_dns_name": "########", "private_ips": [ { @@ -372,20 +383,24 @@ "subregion_name": "us-east-2a", "tags": [ { - "key": "Name", - "value": "Nic-2" + "key": "Key-2", + "value": "value-tags-2" + }, + { + "key": "Key:", + "value": ":value-tags" } ] }, { "account_id": "##id-1##", - "description": "TF-113", + "description": "Primary network interface", "is_source_dest_checked": true, "link_nic": [ { - "delete_on_vm_deletion": false, - "device_number": 1, - "link_nic_id": "##id-2##", + "delete_on_vm_deletion": true, + "device_number": 0, + "link_nic_id": "##id-9##", "state": "attached", "vm_account_id": "##id-1##", "vm_id": "##id-3##" @@ -394,7 +409,7 @@ "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-5##", + "nic_id": "##id-10##", "private_dns_name": "########", "private_ips": [ { @@ -402,12 +417,6 @@ "link_public_ip": [], "private_dns_name": "########", "private_ip": "########" - }, - { - "is_primary": false, - "link_public_ip": [], - "private_dns_name": "########", - "private_ip": "########" } ], "security_groups": [ @@ -419,16 +428,7 @@ "state": "in-use", "subnet_id": "##id-7##", "subregion_name": "us-east-2a", - "tags": [ - { - "key": "Key:", - "value": ":value-tags" - }, - { - "key": "Key-2", - "value": "value-tags-2" - } - ] + "tags": [] } ], "request_id": "########" @@ -453,11 +453,20 @@ "account_id": "##id-1##", "description": "", "is_source_dest_checked": true, - "link_nic": [], + "link_nic": [ + { + "delete_on_vm_deletion": false, + "device_number": 2, + "link_nic_id": "##id-12##", + "state": "attached", + "vm_account_id": "##id-1##", + "vm_id": "##id-3##" + } + ], "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-12##", + "nic_id": "##id-13##", "private_dns_name": "########", "private_ips": [ { @@ -465,6 +474,12 @@ "link_public_ip": [], "private_dns_name": "########", "private_ip": "########" + }, + { + "is_primary": false, + "link_public_ip": [], + "private_dns_name": "########", + "private_ip": "########" } ], "security_groups": [ @@ -473,38 +488,25 @@ "security_group_name": "Terraform-SG-TF113" } ], - "state": "available", - "subnet_id": "##id-13##", - "subregion_name": "us-east-2b", + "state": "in-use", + "subnet_id": "##id-7##", + "subregion_name": "us-east-2a", "tags": [ { - "key": "Key-2", - "value": "value-tags-2" - }, - { - "key": "Key:", - "value": ":value-tags" + "key": "Name", + "value": "Nic-2" } ] }, { "account_id": "##id-1##", - "description": "Primary network interface", + "description": "", "is_source_dest_checked": true, - "link_nic": [ - { - "delete_on_vm_deletion": true, - "device_number": 0, - "link_nic_id": "##id-9##", - "state": "attached", - "vm_account_id": "##id-1##", - "vm_id": "##id-3##" - } - ], + "link_nic": [], "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-10##", + "nic_id": "##id-14##", "private_dns_name": "########", "private_ips": [ { @@ -520,20 +522,29 @@ "security_group_name": "Terraform-SG-TF113" } ], - "state": "in-use", - "subnet_id": "##id-7##", - "subregion_name": "us-east-2a", - "tags": [] + "state": "available", + "subnet_id": "##id-15##", + "subregion_name": "us-east-2b", + "tags": [ + { + "key": "Key:", + "value": ":value-tags" + }, + { + "key": "Key-2", + "value": "value-tags-2" + } + ] }, { "account_id": "##id-1##", - "description": "", + "description": "TF-113", "is_source_dest_checked": true, "link_nic": [ { "delete_on_vm_deletion": false, - "device_number": 2, - "link_nic_id": "##id-14##", + "device_number": 1, + "link_nic_id": "##id-2##", "state": "attached", "vm_account_id": "##id-1##", "vm_id": "##id-3##" @@ -542,7 +553,7 @@ "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-15##", + "nic_id": "##id-5##", "private_dns_name": "########", "private_ips": [ { @@ -569,20 +580,24 @@ "subregion_name": "us-east-2a", "tags": [ { - "key": "Name", - "value": "Nic-2" + "key": "Key:", + "value": ":value-tags" + }, + { + "key": "Key-2", + "value": "value-tags-2" } ] }, { "account_id": "##id-1##", - "description": "TF-113", + "description": "Primary network interface", "is_source_dest_checked": true, "link_nic": [ { - "delete_on_vm_deletion": false, - "device_number": 1, - "link_nic_id": "##id-2##", + "delete_on_vm_deletion": true, + "device_number": 0, + "link_nic_id": "##id-9##", "state": "attached", "vm_account_id": "##id-1##", "vm_id": "##id-3##" @@ -591,7 +606,7 @@ "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-5##", + "nic_id": "##id-10##", "private_dns_name": "########", "private_ips": [ { @@ -599,12 +614,6 @@ "link_public_ip": [], "private_dns_name": "########", "private_ip": "########" - }, - { - "is_primary": false, - "link_public_ip": [], - "private_dns_name": "########", - "private_ip": "########" } ], "security_groups": [ @@ -616,16 +625,7 @@ "state": "in-use", "subnet_id": "##id-7##", "subregion_name": "us-east-2a", - "tags": [ - { - "key": "Key-2", - "value": "value-tags-2" - }, - { - "key": "Key:", - "value": ":value-tags" - } - ] + "tags": [] } ], "request_id": "########" @@ -654,7 +654,7 @@ "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-12##", + "nic_id": "##id-14##", "private_dns_name": "########", "private_ips": [ { @@ -671,7 +671,7 @@ } ], "state": "available", - "subnet_id": "##id-13##", + "subnet_id": "##id-15##", "subregion_name": "us-east-2b", "tags": [ { @@ -795,13 +795,13 @@ "attributes": { "account_id": "##id-1##", "description": "", - "id": "##id-15##", + "id": "##id-13##", "is_source_dest_checked": true, "link_nic": [], "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-15##", + "nic_id": "##id-13##", "private_dns_name": "########", "private_ip": "########", "private_ips": [ @@ -861,13 +861,13 @@ "attributes": { "account_id": "##id-1##", "description": "", - "id": "##id-12##", + "id": "##id-14##", "is_source_dest_checked": true, "link_nic": [], "link_public_ip": [], "mac_address": "########", "net_id": "##id-4##", - "nic_id": "##id-12##", + "nic_id": "##id-14##", "private_dns_name": "########", "private_ip": "########", "private_ips": [ @@ -890,7 +890,7 @@ } ], "state": "available", - "subnet_id": "##id-13##", + "subnet_id": "##id-15##", "subregion_name": "us-east-2b", "tags": [ { @@ -956,9 +956,9 @@ "attributes": { "delete_on_vm_deletion": false, "device_number": 2, - "id": "##id-14##", - "link_nic_id": "##id-14##", - "nic_id": "##id-15##", + "id": "##id-12##", + "link_nic_id": "##id-12##", + "nic_id": "##id-13##", "request_id": "########", "state": null, "vm_account_id": null, @@ -1053,13 +1053,13 @@ "schema_version": 0, "attributes": { "available_ips_count": "########", - "id": "##id-13##", + "id": "##id-15##", "ip_range": "10.5.2.0/24", "map_public_ip_on_launch": false, "net_id": "##id-4##", "request_id": "########", "state": "available", - "subnet_id": "##id-13##", + "subnet_id": "##id-15##", "subregion_name": "us-east-2b", "tags": [], "timeouts": null diff --git a/utils/utils.go b/utils/utils.go index 51fc07512..409888008 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -154,7 +154,7 @@ func StringSliceToFloat32Slice(src []string) (res []float32) { } func LogManuallyDeleted(name, id string) { - log.Printf("[WARN] %s %s not found, probably deleted manually, removing from state", name, id) + log.Printf("\n[WARN] %s %s not found, probably deleted manually, removing from state\n", name, id) } func IsResponseEmpty(len int, name, id string) bool { @@ -167,10 +167,10 @@ func IsResponseEmpty(len int, name, id string) bool { func IsResponseEmptyOrMutiple(rLen int, resName string) error { if rLen == 0 { - return fmt.Errorf("Unable to find %v", resName) + return fmt.Errorf("unable to find %v", resName) } if rLen > 1 { - return fmt.Errorf("Multiple %vs matched; use additional constraints to reduce matches to a single %v", resName, resName) + return fmt.Errorf("multiple %vs matched; use additional constraints to reduce matches to a single %v", resName, resName) } return nil }