diff --git a/azurerm/config.go b/azurerm/config.go index 534323a898ac1..bf647e54d9bf0 100644 --- a/azurerm/config.go +++ b/azurerm/config.go @@ -175,11 +175,7 @@ type ArmClient struct { galleriesClient compute.GalleriesClient galleryImagesClient compute.GalleryImagesClient galleryImageVersionsClient compute.GalleryImageVersionsClient -<<<<<<< HEAD - resourceSkusClient compute.ResourceSkusClient -======= resourceSkusClient compute.ResourceSkusClient ->>>>>>> 5acb4dc0bc503b9c171c6ed56c510cea389c047e snapshotsClient compute.SnapshotsClient usageOpsClient compute.UsageClient vmExtensionImageClient compute.VirtualMachineExtensionImagesClient @@ -532,18 +528,6 @@ func (c *ArmClient) registerComputeClients(endpoint, subscriptionId string, auth galleryImageVersionsClient := compute.NewGalleryImageVersionsClientWithBaseURI(endpoint, subscriptionId) c.configureClient(&galleryImageVersionsClient.Client, auth) c.galleryImageVersionsClient = galleryImageVersionsClient -<<<<<<< HEAD - - resourceSkusClient := compute.NewResourceSkusClientWithBaseURI(endpoint, subscriptionId) - c.configureClient(&resourceSkusClient.Client, auth) - c.resourceSkusClient = resourceSkusClient -} - -func (c *ArmClient) registerDatabricksClients(endpoint, subscriptionId string, auth autorest.Authorizer) { - workspacesClient := databricksSvc.NewWorkspacesClientWithBaseURI(endpoint, subscriptionId) - c.configureClient(&workspacesClient.Client, auth) -======= ->>>>>>> 5acb4dc0bc503b9c171c6ed56c510cea389c047e resourceSkusClient := compute.NewResourceSkusClientWithBaseURI(endpoint, subscriptionId) c.configureClient(&resourceSkusClient.Client, auth) diff --git a/azurerm/data_source_compute_resource_skus.go b/azurerm/data_source_compute_resource_skus.go index cd99800267df4..4fb120b318fc4 100644 --- a/azurerm/data_source_compute_resource_skus.go +++ b/azurerm/data_source_compute_resource_skus.go @@ -3,13 +3,12 @@ package azurerm import ( "fmt" "log" - "strings" //DEBUGGING + "time" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute" "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" - - //"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" ) func dataSourceArmComputeResourceSkus() *schema.Resource { @@ -17,16 +16,176 @@ func dataSourceArmComputeResourceSkus() *schema.Resource { Read: dataSourceArmComputeResourceSkusRead, Schema: map[string]*schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ValidateFunc: validate.NoEmptyStrings, }, + "location": { + Type: schema.TypeString, + Required: true, + }, + "resource_type": { Type: schema.TypeString, Computed: true, }, + + "tier": { + Type: schema.TypeString, + Computed: true, + }, + + "size": { + Type: schema.TypeString, + Computed: true, + }, + + "family": { + Type: schema.TypeString, + Computed: true, + }, + + "kind": { + Type: schema.TypeString, + Computed: true, + }, + + "capacity": { + Type: schema.TypeString, + Computed: true, + }, + + "locations": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "location_info": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "location": { + Type: schema.TypeString, + Computed: true, + }, + + "zones": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + + "api_versions": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + + "costs": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "meter_id": { + Type: schema.TypeString, + Computed: true, + }, + + "quantity": { + Type: schema.TypeInt, + Computed: true, + }, + + "extended_unit": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + + "capabilities": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + + "value": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + + "restrictions": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "restriction_type": { + Type: schema.TypeString, + Computed: true, + }, + + "values": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + + // TODO + // "restriction_info": { + // Type: schema.TypeList, + // Computed: true, + // Elem: &schema.Resource{ + // Schema: map[string]*schema.Schema{ + // "locations": { + // Type: schema.TypeList, + // Computed: true, + // Elem: &schema.Schema{ + // Type: schema.TypeString, + // }, + // }, + + // "zones": { + // Type: schema.TypeList, + // Computed: true, + // Elem: &schema.Schema{ + // Type: schema.TypeString, + // }, + // }, + // }, + // }, + // }, + + // "reason_code": { + // Type: schema.TypeString, + // Computed: true, + // }, + }, + }, + }, + }, }, } } @@ -43,35 +202,184 @@ func dataSourceArmComputeResourceSkusRead(d *schema.ResourceData, meta interface return fmt.Errorf("Error loading Compute Resource SKUs: %+v", err) } - var resourceSku compute.ResourceSku - + filteredResourceSkus := make([]compute.ResourceSku, 0) for resourceSkus.NotDone() { - def := resourceSkus.Value() - if strings.Contains(*def.Name, "D2_V3") { - log.Printf("[FOUND!!] *def.Name: ==%+v==\n", *def.Name) - } + shouldInclude := false + sku := resourceSkus.Value() - if def.Name != nil && *def.Name == name { - log.Printf("[INFO] name: %+v\n", name) - log.Printf("[INFO] def: %+v\n", def) - log.Printf("[INFO] *def.Name: %+v\n", *def.Name) - resourceSku = def - break + if v, ok := d.GetOkExists("location"); ok { + if location := v.(string); location != "" { + if Contains(*sku.Locations, location) && *sku.Name == name { + shouldInclude = true + } + } + } else { + if *sku.Name == name { + shouldInclude = true + } } + if shouldInclude { + filteredResourceSkus = append(filteredResourceSkus, sku) + } err = resourceSkus.NextWithContext(ctx) if err != nil { return fmt.Errorf("Error loading Compute Resource SKUs: %+v", err) } + } + + d.SetId(time.Now().UTC().String()) + d.Set("name", name) + + log.Printf("[DEBUG] filteredResourceSkus: %+v\n", filteredResourceSkus) + + if len(filteredResourceSkus) == 0 { + return fmt.Errorf("Error loading Resource SKU: could not find SKU '%s'. Invalid SKU Name or not valid in this subscription or location.", name) + } else if len(filteredResourceSkus) == 1 { + frs := filteredResourceSkus[0] + + if resource_type := frs.ResourceType; resource_type != nil { + d.Set("resource_type", *frs.ResourceType) + } + + if tier := frs.Tier; tier != nil { + d.Set("tier", *frs.Tier) + } + + if size := frs.Size; size != nil { + d.Set("size", *frs.Size) + } + + if family := frs.Family; family != nil { + d.Set("family", *frs.Family) + } + if kind := frs.Kind; kind != nil { + d.Set("kind", *frs.Kind) + } + + if capacity := frs.Capacity; capacity != nil { + d.Set("capacity", *frs.Capacity) + } + + if frs.LocationInfo != nil { + locationInfo := flattenLocationInfo(frs.LocationInfo) + if err := d.Set("location_info", locationInfo); err != nil { + return fmt.Errorf("Error setting `location_info`: %+v", err) + } + } + + if api_versions := frs.APIVersions; api_versions != nil { + d.Set("api_versions", *frs.APIVersions) + } + + if frs.Costs != nil { + // TODO + } + + if frs.Capabilities != nil { + capabilities := flattenCapabilities(frs.Capabilities) + if err := d.Set("capabilities", capabilities); err != nil { + return fmt.Errorf("Error setting `capabilities`: %+v", err) + } + } + + if frs.Restrictions != nil { + restrictions := flattenRestrictions(frs.Restrictions) + if err := d.Set("restrictions", restrictions); err != nil { + return fmt.Errorf("Error setting `restrictions`: %+v", err) + } + } } - if resourceSku.Name == nil { - return fmt.Errorf("Error loading Resource SKU: could not find SKU '%s'. Invalid or not valid in this subscription.", name) + return nil +} + +func Contains(a []string, x string) bool { + for _, n := range a { + if x == n { + return true + } } + return false +} - d.Set("name", name) - d.Set("resource_type", resourceSku.ResourceType) +func flattenLocationInfo(locationInfo *[]compute.ResourceSkuLocationInfo) []interface{} { + result := make([]interface{}, 0) + if locationInfo == nil { + return result + } - return nil + for _, locInfo := range *locationInfo { + info := make(map[string]interface{}) + + if locInfo.Location != nil { + info["location"] = azure.NormalizeLocation(*locInfo.Location) + } + + zones := make([]string, 0) + if zs := locInfo.Zones; zs != nil { + zones = *zs + } + info["zones"] = zones + + result = append(result, info) + } + return result +} + +func flattenCapabilities(capabilities *[]compute.ResourceSkuCapabilities) []interface{} { + result := make([]interface{}, 0) + if capabilities == nil { + return result + } + + for _, capability := range *capabilities { + cap := make(map[string]interface{}) + + if capability.Name != nil { + cap["name"] = capability.Name + } + + if capability.Value != nil { + cap["value"] = capability.Value + } + + result = append(result, cap) + } + return result +} + +func flattenRestrictions(restrictions *[]compute.ResourceSkuRestrictions) []interface{} { + result := make([]interface{}, 0) + if restrictions == nil { + return result + } + + for _, restriction := range *restrictions { + res := make(map[string]interface{}) + + // TODO + // if restriction.Type != nil { + // res["type"] = restriction.Type + // } + + values := make([]string, 0) + if val := restriction.Values; val != nil { + values = *val + } + res["values"] = values + + if restriction.RestrictionInfo != nil { + res["restriction_info"] = restriction.RestrictionInfo + } + + // TODO + // if restriction.ReasonCode != nil { + // res["reason_code"] = restriction.ReasonCode + // } + + result = append(result, res) + } + return result } diff --git a/azurerm/data_source_compute_resource_skus_test.go b/azurerm/data_source_compute_resource_skus_test.go index ab3c51cdbed38..1ff2ba4ebd60f 100644 --- a/azurerm/data_source_compute_resource_skus_test.go +++ b/azurerm/data_source_compute_resource_skus_test.go @@ -5,6 +5,7 @@ import ( "testing" //"github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/helper/resource" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" ) diff --git a/azurerm/data_source_compute_sku.go b/azurerm/data_source_compute_sku.go deleted file mode 100644 index cfabf4e3f7010..0000000000000 --- a/azurerm/data_source_compute_sku.go +++ /dev/null @@ -1,70 +0,0 @@ -package azurerm - -import ( - "fmt" - - "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute" - "github.com/hashicorp/terraform/helper/schema" - "github.com/hashicorp/terraform/helper/validation" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" -) - -func dataSourceArmComputeResourceSkus() *schema.Resource { - return &schema.Resource{ - Read: dataSourceArmComputeResourceSkusRead, - - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.NoZeroValues, - }, - - "location": azure.SchemaLocationForDataSource(), - }, - } -} - -func dataSourceArmComputeResourceSkusRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*ArmClient).resourceSkusClient - ctx := meta.(*ArmClient).StopContext - - name := d.Get("name").(string) - // location := d.Get("location").(string) - - // var resourceSkus compute.ResourceSkusResultIterator - // var err error - - resourceSkus, err := client.ListComplete(ctx) - - if err != nil { - return fmt.Errorf("Error loading Compute Resource SKUs: %+v", err) - } - - var resourceSku compute.ResourceSku - - for resourceSkus.NotDone() { - def := resourceSkus.Value() - if def.Name != nil && *def.Name == name { - resourceSku = def - break - } - - err = resourceSkus.NextWithContext(ctx) - if err != nil { - return fmt.Errorf("Error loading Compute Resource SKUs: %+v", err) - } - - if resourceSku.Name == nil { - return fmt.Errorf("Error loading Compute Resource SKUs: %+v", err) - } - } - - d.Set("name", resourceSku.Name) - d.Set("location_info", resourceSku.LocationInfo) - // if location := resourceSku.Location; location != nil { - // d.Set("location", azure.NormalizeLocation(*location)) - // } - - return nil -} diff --git a/azurerm/data_source_compute_sku_test.go b/azurerm/data_source_compute_sku_test.go deleted file mode 100644 index 0500cb00b037e..0000000000000 --- a/azurerm/data_source_compute_sku_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package azurerm - -import ( - "fmt" - "testing" - - "github.com/hashicorp/terraform/helper/resource" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" -) - -func TestAccDataSourceAzureRMComputeResourceSkus_basic(t *testing.T) { - ri := tf.AccRandTimeInt() - config := testAccDataSourceAzureRMComputeResourceSkus_basic(ri) - - dataSourceName := "data.azurerm_compute_resource_skus.test" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet(dataSourceName, "name"), - resource.TestCheckResourceAttrSet(dataSourceName, "location"), - ), - }, - }, - }) -} - -func testAccDataSourceAzureRMComputeResourceSkus_basic(rInt int) string { - return fmt.Sprintf(` -data "azurerm_compute_resource_skus" "test" { - name = "${azurerm_compute_resource_skus.test.name}" - location = "${azurerm_compute_resource_skus.test.location}" -} -`, rInt) -} diff --git a/website/docs/d/compute_resource_skus.html.markdown b/website/docs/d/compute_resource_skus.html.markdown index cd7699253c2d0..21ccc1e5fe2bc 100644 --- a/website/docs/d/compute_resource_skus.html.markdown +++ b/website/docs/d/compute_resource_skus.html.markdown @@ -9,28 +9,50 @@ description: |- # Data Source: azurerm_compute_resource_skus -Use this data source to access information about Compute SKUs. +Use this data source to access information about Compute Resource SKUs. ## Example Usage ```hcl data "azurerm_compute_resource_skus" "test" { - name = "Standard_D2s_v3" + name = "Standard_D2s_v2" location = "eastus" } output "location_info" { - value = "${data.azurerm_compute_resource_skus.test.Standard_D2s_v3}" + value = "${data.azurerm_compute_resource_skus.test.location_info}" } ``` ## Argument Reference -* `name` - (Required) The name of the SKU to retreive information for. -* `location` - (Required) The location to retrieve SKU information for. +* `name` - (Required) The name of the SKU to retrieve information for (e.g. Standard_DS2_v2). +* `location` - (Required) The location to retrieve SKU information from. ## Attributes Reference -The following attributes are exported: +The following attributes are exported (Not all attributes may be available for every SKU): -* `location_info` - +* `resource_type` - The type of resource the SKU applies to. + +* `name` - The name of the SKU. + +* `tier` - Specifies the tier of the virtual machines in a scale set. Possible values: **Standard** or **Basic**. + +* `size` - The size of the SKU. + +* `family` - The Family of this particular SKU. + +* `kind` - The Kind of resources that are supported in this SKU. + +* `capacity` - Spcifies the number of virtual machines in the scale set. + +* `location_info` - A list of locations and availability zones in those locations where the SKU is available. + +* `api_versions` - (TODO) The api versions that support this SKU. + +* `costs` - (TODO) Metadata for retrieving price info. + +* `capabilities` - A set of name value pairs describing capabilities. + +* `restrictions` - (TODO) The restrictions because of which SKU cannot be used. This is empty if there are no restrictions. \ No newline at end of file