Skip to content

Commit

Permalink
cleanup PR
Browse files Browse the repository at this point in the history
  • Loading branch information
MattMencel committed Jul 8, 2019
1 parent 230cd99 commit 9c8dd74
Show file tree
Hide file tree
Showing 5 changed files with 483 additions and 1 deletion.
2 changes: 1 addition & 1 deletion azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ type ArmClient struct {
galleriesClient compute.GalleriesClient
galleryImagesClient compute.GalleryImagesClient
galleryImageVersionsClient compute.GalleryImageVersionsClient
resourceSkusClient compute.ResourceSkusClient
resourceSkusClient compute.ResourceSkusClient
snapshotsClient compute.SnapshotsClient
usageOpsClient compute.UsageClient
vmExtensionImageClient compute.VirtualMachineExtensionImagesClient
Expand Down
385 changes: 385 additions & 0 deletions azurerm/data_source_compute_resource_skus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,385 @@
package azurerm

import (
"fmt"
"log"
"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"
)

func dataSourceArmComputeResourceSkus() *schema.Resource {
return &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,
// },
},
},
},
},
},
}
}

func dataSourceArmComputeResourceSkusRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).resourceSkusClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)

resourceSkus, err := client.ListComplete(ctx)

if err != nil {
return fmt.Errorf("Error loading Compute Resource SKUs: %+v", err)
}

filteredResourceSkus := make([]compute.ResourceSku, 0)
for resourceSkus.NotDone() {
shouldInclude := false
sku := resourceSkus.Value()

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)
}

// TODO
// if frs.Costs != nil {
// }

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)
}
}
}

return nil
}

func Contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}

func flattenLocationInfo(locationInfo *[]compute.ResourceSkuLocationInfo) []interface{} {
result := make([]interface{}, 0)
if locationInfo == nil {
return result
}

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
}
Loading

0 comments on commit 9c8dd74

Please sign in to comment.