diff --git a/outscale/data_source_outscale_access_key.go b/outscale/data_source_outscale_access_key.go index 12ec55f53..729f2c779 100644 --- a/outscale/data_source_outscale_access_key.go +++ b/outscale/data_source_outscale_access_key.go @@ -10,41 +10,13 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" "github.com/terraform-providers/terraform-provider-outscale/utils" ) func dataSourceOutscaleAccessKey() *schema.Resource { return &schema.Resource{ - Read: dataSourceOutscaleAccessKeyRead, - Schema: map[string]*schema.Schema{ - "filter": dataSourceFiltersSchema(), - "access_key_id": { - Type: schema.TypeString, - Optional: true, - }, - "creation_date": { - Type: schema.TypeString, - Computed: true, - }, - "expiration_date": { - Type: schema.TypeString, - Computed: true, - }, - "last_modification_date": { - Type: schema.TypeString, - Computed: true, - }, - "state": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "INACTIVE"}, false), - }, - "request_id": { - Type: schema.TypeString, - Computed: true, - }, - }, + Read: dataSourceOutscaleAccessKeyRead, + Schema: getDataSourceSchemas(AccessKeySchema()), } } @@ -52,23 +24,11 @@ func dataSourceOutscaleAccessKeyRead(d *schema.ResourceData, meta interface{}) e conn := meta.(*OutscaleClient).OSCAPI filters, filtersOk := d.GetOk("filter") - accessKeyID, accessKeyOk := d.GetOk("access_key_id") - state, stateOk := d.GetOk("state") - - if !filtersOk && !accessKeyOk && !stateOk { - return fmt.Errorf("One of filters, access_key_id or state must be assigned") - } filterReq := &oscgo.FiltersAccessKeys{} if filtersOk { filterReq = buildOutscaleDataSourceAccessKeyFilters(filters.(*schema.Set)) } - if accessKeyOk { - filterReq.SetAccessKeyIds([]string{accessKeyID.(string)}) - } - if stateOk { - filterReq.SetStates([]string{state.(string)}) - } var resp oscgo.ReadAccessKeysResponse var err error diff --git a/outscale/data_source_outscale_access_key_test.go b/outscale/data_source_outscale_access_key_test.go index 981ad247a..b2ae9a78c 100644 --- a/outscale/data_source_outscale_access_key_test.go +++ b/outscale/data_source_outscale_access_key_test.go @@ -6,31 +6,9 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) -func TestAccOutscaleDataSourceAccessKey_basic(t *testing.T) { - t.Parallel() - dataSourceName := "outscale_access_key.outscale_access_key" - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccClientAccessKeyDataSourceBasic(), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet(dataSourceName, "access_key_id"), - resource.TestCheckResourceAttrSet(dataSourceName, "creation_date"), - resource.TestCheckResourceAttrSet(dataSourceName, "last_modification_date"), - resource.TestCheckResourceAttrSet(dataSourceName, "secret_key"), - resource.TestCheckResourceAttrSet(dataSourceName, "state"), - ), - }, - }, - }) -} - func TestAccOutscaleDataSourceAccessKey_withFilters(t *testing.T) { t.Parallel() - dataSourceName := "outscale_access_key.outscale_access_key" + dataSourceName := "outscale_access_key.access_key" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -50,21 +28,11 @@ func TestAccOutscaleDataSourceAccessKey_withFilters(t *testing.T) { }) } -func testAccClientAccessKeyDataSourceBasic() string { - return ` - resource "outscale_access_key" "outscale_access_key" {} - - data "outscale_access_key" "outscale_access_key" { - access_key_id = outscale_access_key.outscale_access_key.id - } - ` -} - func testAccClientAccessKeyDataSourceWithFilters() string { return ` resource "outscale_access_key" "outscale_access_key" {} - data "outscale_access_key" "outscale_access_key" { + data "outscale_access_key" "access_key" { filter { name = "access_key_ids" values = [outscale_access_key.outscale_access_key.id] diff --git a/outscale/data_source_outscale_access_keys.go b/outscale/data_source_outscale_access_keys.go index 1d1aa7518..8d96f2973 100644 --- a/outscale/data_source_outscale_access_keys.go +++ b/outscale/data_source_outscale_access_keys.go @@ -9,61 +9,13 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" "github.com/terraform-providers/terraform-provider-outscale/utils" ) func dataSourceOutscaleAccessKeys() *schema.Resource { return &schema.Resource{ - Read: dataSourceOutscaleAccessKeysRead, - Schema: map[string]*schema.Schema{ - "filter": dataSourceFiltersSchema(), - "access_key_ids": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "states": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Schema{ - Type: schema.TypeString, - ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "INACTIVE"}, false), - }, - }, - "access_keys": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "access_key_id": { - Type: schema.TypeString, - Computed: true, - }, - "creation_date": { - Type: schema.TypeString, - Computed: true, - }, - "expiration_date": { - Type: schema.TypeString, - Computed: true, - }, - "last_modification_date": { - Type: schema.TypeString, - Computed: true, - }, - "state": { - Type: schema.TypeString, - Computed: true, - }, - }, - }, - }, - "request_id": { - Type: schema.TypeString, - Computed: true, - }, - }, + Read: dataSourceOutscaleAccessKeysRead, + Schema: GetDataSourcesSchema("access_keys", AccessKeySchema()), } } @@ -71,23 +23,11 @@ func dataSourceOutscaleAccessKeysRead(d *schema.ResourceData, meta interface{}) conn := meta.(*OutscaleClient).OSCAPI filters, filtersOk := d.GetOk("filter") - accessKeyID, accessKeyOk := d.GetOk("access_key_ids") - state, stateOk := d.GetOk("states") - - if !filtersOk && !accessKeyOk && !stateOk { - return fmt.Errorf("One of filters, access_key_ids or states must be assigned") - } filterReq := &oscgo.FiltersAccessKeys{} if filtersOk { filterReq = buildOutscaleDataSourceAccessKeyFilters(filters.(*schema.Set)) } - if accessKeyOk { - filterReq.SetAccessKeyIds(expandStringValueList(accessKeyID.([]interface{}))) - } - if stateOk { - filterReq.SetStates(expandStringValueList(state.([]interface{}))) - } var resp oscgo.ReadAccessKeysResponse var err error diff --git a/outscale/data_source_outscale_access_keys_test.go b/outscale/data_source_outscale_access_keys_test.go index 170987e65..559ceabc8 100644 --- a/outscale/data_source_outscale_access_keys_test.go +++ b/outscale/data_source_outscale_access_keys_test.go @@ -6,59 +6,38 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) -func TestAccOutscaleDataSourceAccessKeys_basic(t *testing.T) { +func TestAccOutscaleDataSourceAccessKeys(t *testing.T) { t.Parallel() - dataSourceName := "data.outscale_access_keys.outscale_access_key" + dataSourceName := "data.outscale_access_keys.access_keys_with_filter" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccClientAccessKeysDataSourceBasic(), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet(dataSourceName, "access_keys.#"), - resource.TestCheckResourceAttrSet(dataSourceName, "access_key_ids.#"), - ), - }, - }, - }) -} - -func TestAccOutscaleDataSourceAccessKeys_withFilters(t *testing.T) { - t.Parallel() - dataSourceName := "data.outscale_access_keys.outscale_access_key" - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccClientAccessKeysDataSourceWithFilters(), + Config: testAccClientAccessKeysDataSource(), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(dataSourceName, "access_keys.#"), resource.TestCheckResourceAttrSet(dataSourceName, "filter.#"), + resource.TestCheckResourceAttrSet("data.outscale_access_keys.access_keys", "access_keys.#"), ), }, }, }) } -func testAccClientAccessKeysDataSourceBasic() string { +func testAccClientAccessKeysDataSource() string { return ` resource "outscale_access_key" "outscale_access_key" {} - data "outscale_access_keys" "outscale_access_key" { - access_key_ids = ["${outscale_access_key.outscale_access_key.id}"] + data "outscale_access_keys" "access_keys_with_filter" { + filter { + name = "access_key_ids" + values = ["${outscale_access_key.outscale_access_key.id}"] + } } - ` -} - -func testAccClientAccessKeysDataSourceWithFilters() string { - return ` - resource "outscale_access_key" "outscale_access_key" {} - data "outscale_access_keys" "outscale_access_key" { + data "outscale_access_keys" "access_keys" { filter { name = "access_key_ids" values = ["${outscale_access_key.outscale_access_key.id}"] diff --git a/outscale/data_source_outscale_vm.go b/outscale/data_source_outscale_vm.go index 6bb83cede..38fca4a6b 100644 --- a/outscale/data_source_outscale_vm.go +++ b/outscale/data_source_outscale_vm.go @@ -17,7 +17,7 @@ import ( func dataSourceOutscaleOAPIVM() *schema.Resource { return &schema.Resource{ Read: dataSourceOutscaleOAPIVMRead, - Schema: getDataSourceOAPIVMSchemas(), + Schema: GetDataSourceSchema(VMSchema()), } } func dataSourceOutscaleOAPIVMRead(d *schema.ResourceData, meta interface{}) error { @@ -229,25 +229,6 @@ func getOAPIVMSecurityGroups(groupSet []oscgo.SecurityGroupLight) []map[string]i return res } -func getDataSourceOAPIVMSchemas() map[string]*schema.Schema { - wholeSchema := map[string]*schema.Schema{ - "filter": dataSourceFiltersSchema(), - } - - attrsSchema := getOApiVMAttributesSchema() - - for k, v := range attrsSchema { - wholeSchema[k] = v - } - - wholeSchema["request_id"] = &schema.Schema{ - Type: schema.TypeString, - Computed: true, - } - - return wholeSchema -} - func buildOutscaleOAPIDataSourceVMFilters(set *schema.Set) *oscgo.FiltersVm { filters := new(oscgo.FiltersVm) @@ -273,419 +254,3 @@ func buildOutscaleOAPIDataSourceVMFilters(set *schema.Set) *oscgo.FiltersVm { } return filters } - -func getOApiVMAttributesSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - // Attributes - "architecture": { - Type: schema.TypeString, - Computed: true, - }, - "block_device_mappings_created": { - Type: schema.TypeList, - Optional: true, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "bsu": { - Type: schema.TypeMap, - Optional: true, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "delete_on_vm_deletion": { - Type: schema.TypeBool, - Computed: true, - }, - "link_date": { - Type: schema.TypeString, - Computed: true, - }, - "state": { - Type: schema.TypeInt, - Computed: true, - }, - "volume_id": { - Type: schema.TypeFloat, - Computed: true, - }, - }, - }, - }, - "device_name": { - Type: schema.TypeString, - Optional: true, - }, - }, - }, - }, - "bsu_optimized": { - Type: schema.TypeBool, - Optional: true, - Computed: true, - }, - "client_token": { - Type: schema.TypeString, - Computed: true, - }, - "creation_date": { - Type: schema.TypeString, - Computed: true, - }, - "deletion_protection": { - Type: schema.TypeBool, - Optional: true, - Computed: true, - }, - "hypervisor": { - Type: schema.TypeString, - Computed: true, - }, - "image_id": { - Type: schema.TypeString, - ForceNew: true, - Optional: true, - Computed: true, - }, - "is_source_dest_checked": { - Type: schema.TypeBool, - Optional: true, - Computed: true, - }, - "keypair_name": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "security_group_ids": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "security_group_names": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "launch_number": { - Type: schema.TypeInt, - Computed: true, - }, - "nested_virtualization": { - Type: schema.TypeBool, - Computed: true, - }, - "net_id": { - Type: schema.TypeString, - Computed: true, - }, - "nics": { - Type: schema.TypeList, - Optional: true, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "delete_on_vm_deletion": { - Type: schema.TypeBool, - Computed: true, - Optional: true, - }, - "description": { - Type: schema.TypeString, - Computed: true, - Optional: true, - }, - "device_number": { - Type: schema.TypeInt, - Computed: true, - Optional: true, - }, - "nic_id": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "private_ips": { - Type: schema.TypeSet, - Optional: true, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "is_primary": { - Type: schema.TypeBool, - Optional: true, - Computed: true, - }, - "link_public_ip": { - Type: schema.TypeSet, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "public_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "public_ip": { - Type: schema.TypeString, - Computed: true, - }, - "public_ip_account_id": { - Type: schema.TypeString, - Computed: true, - }, - }, - }, - }, - "private_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "private_ip": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - }, - }, - }, - "secondary_private_ip_count": { - Type: schema.TypeInt, - Optional: true, - Computed: true, - }, - "security_group_ids": { - Type: schema.TypeList, - Optional: true, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "account_id": { - Type: schema.TypeString, - Computed: true, - }, - - "is_source_dest_checked": { - Type: schema.TypeBool, - Computed: true, - }, - - "subnet_id": { - Type: schema.TypeString, - Computed: true, - Optional: true, - }, - "link_nic": { - Type: schema.TypeList, - Computed: true, - MaxItems: 1, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "delete_on_vm_deletion": { - Type: schema.TypeBool, - Computed: true, - }, - "device_number": { - Type: schema.TypeString, - Computed: true, - }, - "link_nic_id": { - Type: schema.TypeString, - Computed: true, - }, - "state": { - Type: schema.TypeString, - Computed: true, - }, - }, - }, - }, - "link_public_ip": { - Type: schema.TypeSet, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "public_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "public_ip": { - Type: schema.TypeString, - Computed: true, - }, - "public_ip_account_id": { - Type: schema.TypeString, - Computed: true, - }, - }, - }, - }, - "mac_address": { - Type: schema.TypeString, - Computed: true, - }, - "net_id": { - Type: schema.TypeString, - Computed: true, - }, - - "private_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "security_groups_names": { - Type: schema.TypeList, - Optional: true, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "security_groups": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "security_group_id": { - Type: schema.TypeString, - Computed: true, - }, - "security_group_name": { - Type: schema.TypeString, - Computed: true, - }, - }, - }, - }, - "state": { - Type: schema.TypeString, - Computed: true, - }, - }, - }, - }, - "os_family": { - Type: schema.TypeString, - Computed: true, - }, - "performance": { - Type: schema.TypeString, - Computed: true, - }, - "placement_subregion_name": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "placement_tenancy": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "private_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "private_ip": { - Type: schema.TypeString, - Computed: true, - }, - "product_codes": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "public_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "public_ip": { - Type: schema.TypeString, - Computed: true, - }, - "reservation_id": { - Type: schema.TypeString, - Computed: true, - }, - "root_device_name": { - Type: schema.TypeString, - Computed: true, - }, - "root_device_type": { - Type: schema.TypeString, - Computed: true, - }, - "security_groups": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "security_group_id": { - Type: schema.TypeString, - Computed: true, - }, - "security_group_name": { - Type: schema.TypeString, - Computed: true, - }, - }, - }, - }, - "state": { - Type: schema.TypeString, - Computed: true, - }, - "state_reason": { - Type: schema.TypeString, - Computed: true, - }, - "subnet_id": { - Type: schema.TypeString, - ForceNew: true, - Optional: true, - Computed: true, - }, - "tags": { - Type: schema.TypeList, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "key": { - Type: schema.TypeString, - Computed: true, - }, - "value": { - Type: schema.TypeString, - Computed: true, - }, - }, - }, - Computed: true, - }, - "user_data": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "vm_id": { - Type: schema.TypeString, - Computed: true, - Optional: true, - }, - "vm_initiated_shutdown_behavior": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "vm_type": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "request_id": { - Type: schema.TypeString, - Computed: true, - }, - "private_ips": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - } -} diff --git a/outscale/data_source_outscale_vms.go b/outscale/data_source_outscale_vms.go index 4cefb246d..a39761e1e 100644 --- a/outscale/data_source_outscale_vms.go +++ b/outscale/data_source_outscale_vms.go @@ -17,51 +17,10 @@ func datasourceOutscaleOApiVMS() *schema.Resource { return &schema.Resource{ Read: dataSourceOutscaleOApiVMSRead, - Schema: datasourceOutscaleOApiVMSSchema(), + Schema: GetDataSourcesSchema("vms", VMSchema()), } } -func dataSourceFiltersSchema() *schema.Schema { - return &schema.Schema{ - Type: schema.TypeSet, - Optional: true, - ForceNew: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - }, - - "values": { - Type: schema.TypeList, - Required: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - }, - }, - } -} - -func datasourceOutscaleOApiVMSSchema() map[string]*schema.Schema { - wholeSchema := map[string]*schema.Schema{ - "filter": dataSourceFiltersSchema(), - "vms": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Resource{ - Schema: getOApiVMAttributesSchema(), - }, - }, - "request_id": { - Type: schema.TypeString, - Computed: true, - }, - } - - return wholeSchema -} - func dataSourceOutscaleOApiVMSRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*OutscaleClient).OSCAPI diff --git a/outscale/data_source_outscale_vms_test.go b/outscale/data_source_outscale_vms_test.go index 821808c34..662f30638 100644 --- a/outscale/data_source_outscale_vms_test.go +++ b/outscale/data_source_outscale_vms_test.go @@ -34,7 +34,7 @@ func testAccOAPIVMSDataSourceConfig(omi, vmType string) string { resource "outscale_vm" "basic" { image_id = "%s" vm_type = "%s" - keypair_name = "terraform-basic" + #keypair_name = "terraform-basic" } data "outscale_vms" "basic_web" { diff --git a/outscale/resource_outscale_access_key.go b/outscale/resource_outscale_access_key.go index 86c3b6d72..ec3e0dd1e 100644 --- a/outscale/resource_outscale_access_key.go +++ b/outscale/resource_outscale_access_key.go @@ -13,6 +13,42 @@ import ( "github.com/terraform-providers/terraform-provider-outscale/utils" ) +func AccessKeySchema() map[string]*schema.Schema { + return map[string]*schema.Schema{ + "access_key_id": { + Type: schema.TypeString, + Computed: true, + }, + "creation_date": { + Type: schema.TypeString, + Computed: true, + }, + "expiration_date": { + Type: schema.TypeString, + Optional: true, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + date1, _ := datetime.Parse(new, time.UTC) + date2, _ := datetime.Parse(old, time.UTC) + return date1.Equal(date2) + }, + }, + "last_modification_date": { + Type: schema.TypeString, + Computed: true, + }, + "secret_key": { + Type: schema.TypeString, + Computed: true, + }, + "state": { + Type: schema.TypeString, + Optional: true, + Default: "ACTIVE", + ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "INACTIVE"}, false), + }, + } +} + func resourceOutscaleAccessKey() *schema.Resource { return &schema.Resource{ Create: resourceOutscaleAccessKeyCreate, @@ -22,44 +58,7 @@ func resourceOutscaleAccessKey() *schema.Resource { Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, - - Schema: map[string]*schema.Schema{ - "access_key_id": { - Type: schema.TypeString, - Computed: true, - }, - "creation_date": { - Type: schema.TypeString, - Computed: true, - }, - "expiration_date": { - Type: schema.TypeString, - Optional: true, - DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { - date1, _ := datetime.Parse(new, time.UTC) - date2, _ := datetime.Parse(old, time.UTC) - return date1.Equal(date2) - }, - }, - "last_modification_date": { - Type: schema.TypeString, - Computed: true, - }, - "secret_key": { - Type: schema.TypeString, - Computed: true, - }, - "state": { - Type: schema.TypeString, - Optional: true, - Default: "ACTIVE", - ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "INACTIVE"}, false), - }, - "request_id": { - Type: schema.TypeString, - Computed: true, - }, - }, + Schema: GetResourceSchema(AccessKeySchema()), } } diff --git a/outscale/resource_outscale_vm.go b/outscale/resource_outscale_vm.go index 17c0b745e..328d0d707 100644 --- a/outscale/resource_outscale_vm.go +++ b/outscale/resource_outscale_vm.go @@ -18,506 +18,506 @@ import ( "github.com/terraform-providers/terraform-provider-outscale/utils" ) -func resourceOutscaleOApiVM() *schema.Resource { - return &schema.Resource{ - Create: resourceOAPIVMCreate, - Read: resourceOAPIVMRead, - Update: resourceOAPIVMUpdate, - Delete: resourceOAPIVMDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, - Timeouts: &schema.ResourceTimeout{ - Create: schema.DefaultTimeout(10 * time.Minute), - Update: schema.DefaultTimeout(10 * time.Minute), - Delete: schema.DefaultTimeout(10 * time.Minute), - }, - - Schema: map[string]*schema.Schema{ - "block_device_mappings": { - Type: schema.TypeList, - Optional: true, - //ForceNew: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "bsu": { - Type: schema.TypeList, - Optional: true, - Computed: true, - MaxItems: 1, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "delete_on_vm_deletion": { - Type: schema.TypeBool, - Optional: true, - }, - "iops": { - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { - iopsVal := val.(int) - if iopsVal < utils.MinIops || iopsVal > utils.MaxIops { - errs = append(errs, fmt.Errorf("%q must be between %d and %d inclusive, got: %d", key, utils.MinIops, utils.MaxIops, iopsVal)) - } - return - }, - }, - "snapshot_id": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - "volume_size": { - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { - vSize := val.(int) - if vSize < 1 || vSize > utils.MaxSize { - errs = append(errs, fmt.Errorf("%q must be between 1 and %d gibibytes inclusive, got: %d", key, utils.MaxSize, vSize)) - } - return - }, +func VMSchema() map[string]*schema.Schema { + return map[string]*schema.Schema{ + "block_device_mappings": { + Type: schema.TypeList, + Optional: true, + //ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "bsu": { + Type: schema.TypeList, + Optional: true, + Computed: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "delete_on_vm_deletion": { + Type: schema.TypeBool, + Optional: true, + }, + "iops": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { + iopsVal := val.(int) + if iopsVal < utils.MinIops || iopsVal > utils.MaxIops { + errs = append(errs, fmt.Errorf("%q must be between %d and %d inclusive, got: %d", key, utils.MinIops, utils.MaxIops, iopsVal)) + } + return }, - "volume_type": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, + }, + "snapshot_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "volume_size": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { + vSize := val.(int) + if vSize < 1 || vSize > utils.MaxSize { + errs = append(errs, fmt.Errorf("%q must be between 1 and %d gibibytes inclusive, got: %d", key, utils.MaxSize, vSize)) + } + return }, }, + "volume_type": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, }, - "device_name": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - "no_device": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - "virtual_device_name": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, + }, + "device_name": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "no_device": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "virtual_device_name": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, }, }, }, - "bsu_optimized": { - Type: schema.TypeBool, - Optional: true, - Computed: true, - }, - "client_token": { - Type: schema.TypeString, - Computed: true, - }, - "creation_date": { - Type: schema.TypeString, - Computed: true, - }, - "deletion_protection": { - Type: schema.TypeBool, - Optional: true, - Computed: true, - }, - "image_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "keypair_name": { - Type: schema.TypeString, - Optional: true, - Computed: true, + }, + "bsu_optimized": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, + "client_token": { + Type: schema.TypeString, + Computed: true, + }, + "creation_date": { + Type: schema.TypeString, + Computed: true, + }, + "deletion_protection": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, + "image_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "keypair_name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "nics": { + Type: schema.TypeSet, + Optional: true, + Computed: true, + ForceNew: true, + Set: func(v interface{}) int { + return v.(map[string]interface{})["device_number"].(int) }, - "nics": { - Type: schema.TypeSet, - Optional: true, - Computed: true, - ForceNew: true, - Set: func(v interface{}) int { - return v.(map[string]interface{})["device_number"].(int) - }, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "delete_on_vm_deletion": { - Type: schema.TypeBool, - Optional: true, - Computed: true, - ForceNew: true, - }, - "description": { - Type: schema.TypeString, - Computed: true, - Optional: true, - ForceNew: true, - }, - "device_number": { - Type: schema.TypeInt, - Required: true, - ForceNew: true, - }, - "nic_id": { - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: true, - }, - "private_ips": { - Type: schema.TypeSet, - Optional: true, - Computed: true, - ForceNew: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "is_primary": { - Type: schema.TypeBool, - Optional: true, - Computed: true, - ForceNew: true, - }, - "link_public_ip": { - Type: schema.TypeSet, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "public_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "public_ip": { - Type: schema.TypeString, - Computed: true, - }, - "public_ip_account_id": { - Type: schema.TypeString, - Computed: true, - }, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "delete_on_vm_deletion": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + ForceNew: true, + }, + "description": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, + "device_number": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + "nic_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "private_ips": { + Type: schema.TypeSet, + Optional: true, + Computed: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "is_primary": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + ForceNew: true, + }, + "link_public_ip": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "public_dns_name": { + Type: schema.TypeString, + Computed: true, + }, + "public_ip": { + Type: schema.TypeString, + Computed: true, + }, + "public_ip_account_id": { + Type: schema.TypeString, + Computed: true, }, }, }, - "private_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "private_ip": { - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: true, - }, + }, + "private_dns_name": { + Type: schema.TypeString, + Computed: true, + }, + "private_ip": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, }, }, }, - "secondary_private_ip_count": { - Type: schema.TypeInt, - Optional: true, - Computed: true, - ForceNew: true, - }, - "account_id": { - Type: schema.TypeString, - Computed: true, - }, + }, + "secondary_private_ip_count": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + ForceNew: true, + }, + "account_id": { + Type: schema.TypeString, + Computed: true, + }, - "is_source_dest_checked": { - Type: schema.TypeBool, - Computed: true, - }, + "is_source_dest_checked": { + Type: schema.TypeBool, + Computed: true, + }, - "subnet_id": { - Type: schema.TypeString, - Computed: true, - Optional: true, - ForceNew: true, - }, - "link_nic": { - Type: schema.TypeList, - MaxItems: 1, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "delete_on_vm_deletion": { - Type: schema.TypeBool, - Computed: true, - }, - "device_number": { - Type: schema.TypeString, - Computed: true, - }, - "link_nic_id": { - Type: schema.TypeString, - Computed: true, - }, - "state": { - Type: schema.TypeString, - Computed: true, - }, + "subnet_id": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, + "link_nic": { + Type: schema.TypeList, + MaxItems: 1, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "delete_on_vm_deletion": { + Type: schema.TypeBool, + Computed: true, + }, + "device_number": { + Type: schema.TypeString, + Computed: true, + }, + "link_nic_id": { + Type: schema.TypeString, + Computed: true, + }, + "state": { + Type: schema.TypeString, + Computed: true, }, }, }, - "link_public_ip": { - Type: schema.TypeSet, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "public_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "public_ip": { - Type: schema.TypeString, - Computed: true, - }, - "public_ip_account_id": { - Type: schema.TypeString, - Computed: true, - }, + }, + "link_public_ip": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "public_dns_name": { + Type: schema.TypeString, + Computed: true, + }, + "public_ip": { + Type: schema.TypeString, + Computed: true, + }, + "public_ip_account_id": { + Type: schema.TypeString, + Computed: true, }, }, }, - "mac_address": { - Type: schema.TypeString, - Computed: true, - }, - "net_id": { - Type: schema.TypeString, - Computed: true, - }, + }, + "mac_address": { + Type: schema.TypeString, + Computed: true, + }, + "net_id": { + Type: schema.TypeString, + Computed: true, + }, - "private_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "security_group_ids": { - Type: schema.TypeList, - Optional: true, - ForceNew: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "security_groups": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "security_group_id": { - Type: schema.TypeString, - Computed: true, - }, - "security_group_name": { - Type: schema.TypeString, - Computed: true, - }, + "private_dns_name": { + Type: schema.TypeString, + Computed: true, + }, + "security_group_ids": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "security_groups": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "security_group_id": { + Type: schema.TypeString, + Computed: true, + }, + "security_group_name": { + Type: schema.TypeString, + Computed: true, }, }, }, - "state": { - Type: schema.TypeString, - Computed: true, - }, + }, + "state": { + Type: schema.TypeString, + Computed: true, }, }, }, - "placement_subregion_name": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "placement_tenancy": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "private_ips": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "security_group_ids": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "security_group_names": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "subnet_id": { - Type: schema.TypeString, - ForceNew: true, - Optional: true, - Computed: true, - }, + }, + "placement_subregion_name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "placement_tenancy": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "private_ips": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "security_group_ids": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "security_group_names": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "subnet_id": { + Type: schema.TypeString, + ForceNew: true, + Optional: true, + Computed: true, + }, - "security_groups": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "security_group_id": { - Type: schema.TypeString, - Computed: true, - }, - "security_group_name": { - Type: schema.TypeString, - Computed: true, - }, + "security_groups": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "security_group_id": { + Type: schema.TypeString, + Computed: true, + }, + "security_group_name": { + Type: schema.TypeString, + Computed: true, }, }, }, - "architecture": { - Type: schema.TypeString, - Computed: true, - }, - "block_device_mappings_created": { - Type: schema.TypeList, - Optional: true, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "bsu": { - Type: schema.TypeMap, - Optional: true, - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "delete_on_vm_deletion": { - Type: schema.TypeBool, - Computed: true, - }, - "link_date": { - Type: schema.TypeString, - Computed: true, - }, - "state": { - Type: schema.TypeInt, - Computed: true, - }, - "volume_id": { - Type: schema.TypeFloat, - Computed: true, - }, + }, + "architecture": { + Type: schema.TypeString, + Computed: true, + }, + "block_device_mappings_created": { + Type: schema.TypeList, + Optional: true, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "bsu": { + Type: schema.TypeMap, + Optional: true, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "delete_on_vm_deletion": { + Type: schema.TypeBool, + Computed: true, + }, + "link_date": { + Type: schema.TypeString, + Computed: true, + }, + "state": { + Type: schema.TypeInt, + Computed: true, + }, + "volume_id": { + Type: schema.TypeFloat, + Computed: true, }, }, }, - "device_name": { - Type: schema.TypeString, - Optional: true, - }, + }, + "device_name": { + Type: schema.TypeString, + Optional: true, }, }, }, - "hypervisor": { - Type: schema.TypeString, - Computed: true, - }, - "is_source_dest_checked": { - Type: schema.TypeBool, - Optional: true, - Computed: true, - }, - "launch_number": { - Type: schema.TypeInt, - Computed: true, - }, - "nested_virtualization": { - Type: schema.TypeBool, - Optional: true, - Default: false, - }, - "net_id": { - Type: schema.TypeString, - Computed: true, - }, - "os_family": { - Type: schema.TypeString, - Computed: true, - }, - "performance": { - Type: schema.TypeString, - Computed: true, - Optional: true, - }, - "private_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "private_ip": { - Type: schema.TypeString, - Computed: true, - }, - "product_codes": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - "public_dns_name": { - Type: schema.TypeString, - Computed: true, - }, - "public_ip": { - Type: schema.TypeString, - Computed: true, - }, - "reservation_id": { - Type: schema.TypeString, - Computed: true, - }, - "root_device_name": { - Type: schema.TypeString, - Computed: true, - }, - "root_device_type": { - Type: schema.TypeString, - Computed: true, - }, - "state": { - Type: schema.TypeString, - Optional: true, - Default: "running", - }, - "state_reason": { - Type: schema.TypeString, - Computed: true, - }, - "user_data": { - Type: schema.TypeString, - Optional: true, - }, - "vm_id": { - Type: schema.TypeString, - Computed: true, - Optional: true, - }, - "vm_initiated_shutdown_behavior": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "vm_type": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "request_id": { - Type: schema.TypeString, - Computed: true, - }, - "admin_password": { - Type: schema.TypeString, - Computed: true, - }, - "get_admin_password": { - Type: schema.TypeBool, - Optional: true, - }, - "tags": tagsListOAPISchema(), }, + "hypervisor": { + Type: schema.TypeString, + Computed: true, + }, + "is_source_dest_checked": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, + "launch_number": { + Type: schema.TypeInt, + Computed: true, + }, + "nested_virtualization": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + "net_id": { + Type: schema.TypeString, + Computed: true, + }, + "os_family": { + Type: schema.TypeString, + Computed: true, + }, + "performance": { + Type: schema.TypeString, + Computed: true, + Optional: true, + }, + "private_dns_name": { + Type: schema.TypeString, + Computed: true, + }, + "private_ip": { + Type: schema.TypeString, + Computed: true, + }, + "product_codes": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "public_dns_name": { + Type: schema.TypeString, + Computed: true, + }, + "public_ip": { + Type: schema.TypeString, + Computed: true, + }, + "reservation_id": { + Type: schema.TypeString, + Computed: true, + }, + "root_device_name": { + Type: schema.TypeString, + Computed: true, + }, + "root_device_type": { + Type: schema.TypeString, + Computed: true, + }, + "state": { + Type: schema.TypeString, + Optional: true, + Default: "running", + }, + "state_reason": { + Type: schema.TypeString, + Computed: true, + }, + "user_data": { + Type: schema.TypeString, + Optional: true, + }, + "vm_id": { + Type: schema.TypeString, + Computed: true, + Optional: true, + }, + "vm_initiated_shutdown_behavior": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "vm_type": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "admin_password": { + Type: schema.TypeString, + Computed: true, + }, + "get_admin_password": { + Type: schema.TypeBool, + Optional: true, + }, + "tags": tagsListOAPISchema(), + } +} + +func resourceOutscaleOApiVM() *schema.Resource { + return &schema.Resource{ + Create: resourceOAPIVMCreate, + Read: resourceOAPIVMRead, + Update: resourceOAPIVMUpdate, + Delete: resourceOAPIVMDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(10 * time.Minute), + Update: schema.DefaultTimeout(10 * time.Minute), + Delete: schema.DefaultTimeout(10 * time.Minute), + }, + + Schema: GetResourceSchema(VMSchema()), } } diff --git a/outscale/schema.go b/outscale/schema.go new file mode 100644 index 000000000..9e983a4f2 --- /dev/null +++ b/outscale/schema.go @@ -0,0 +1,84 @@ +package outscale + +import ( + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func GetDataSourcesSchema(field string, resourceSchema map[string]*schema.Schema) map[string]*schema.Schema { + result := map[string]*schema.Schema{ + field: { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: setComputed(resourceSchema), + }, + }, + } + result = addFilter(result) + result = addRequestId(result) + return result +} + +func GetDataSourceSchema(resourceSchema map[string]*schema.Schema) map[string]*schema.Schema { + result := setComputed(resourceSchema) + result = addFilter(result) + result = addRequestId(result) + return result +} + +func GetResourceSchema(resourceSchema map[string]*schema.Schema) map[string]*schema.Schema { + result := addRequestId(resourceSchema) + return result +} + +func addFilter(resourceSchema map[string]*schema.Schema) map[string]*schema.Schema { + resourceSchema["filter"] = dataSourceFiltersSchema() + return resourceSchema +} + +func addRequestId(resourceSchema map[string]*schema.Schema) map[string]*schema.Schema { + resourceSchema["request_id"] = requestIdSchema() + return resourceSchema +} +func setComputed(resourceSchema map[string]*schema.Schema) map[string]*schema.Schema { + for k, v := range resourceSchema { + v.Computed = true + v.Required = false + v.Optional = false + v.ForceNew = false + v.Default = nil + v.DiffSuppressFunc = nil + v.ValidateFunc = nil + resourceSchema[k] = v + } + return resourceSchema +} + +func requestIdSchema() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeString, + Computed: true, + } +} + +func dataSourceFiltersSchema() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + + "values": { + Type: schema.TypeList, + Required: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + } +}