Skip to content

Commit

Permalink
azurerm_api_management_api - deprecate sku in favour of the `sku_na…
Browse files Browse the repository at this point in the history
…me` property (#3154)

* Flatten arm_api_management sku

* Updates for backwards compatibility

* Updated test cases

* Addressing some PR comments

* Fix for test pass

* Rmoved comments and fixed docs

* Fixed ImportStateVerify

* Added SKU back into datasource

* Updated code per PR comments

* Removed newline

* Remove unused function
  • Loading branch information
WodansSon authored Sep 24, 2019
1 parent e3dc821 commit a1d0ce1
Show file tree
Hide file tree
Showing 22 changed files with 360 additions and 164 deletions.
33 changes: 14 additions & 19 deletions azurerm/data_source_api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func dataSourceApiManagementService() *schema.Resource {
Computed: true,
},

// TODO: Remove in 2.0
"sku": {
Type: schema.TypeList,
Computed: true,
Expand All @@ -57,6 +58,11 @@ func dataSourceApiManagementService() *schema.Resource {
},
},

"sku_name": {
Type: schema.TypeString,
Computed: true,
},

"notification_sender_email": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -181,7 +187,6 @@ func dataSourceApiManagementRead(d *schema.ResourceData, meta interface{}) error
if props := resp.ServiceProperties; props != nil {
d.Set("publisher_email", props.PublisherEmail)
d.Set("publisher_name", props.PublisherName)

d.Set("notification_sender_email", props.NotificationSenderEmail)
d.Set("gateway_url", props.GatewayURL)
d.Set("gateway_regional_url", props.GatewayRegionalURL)
Expand All @@ -199,8 +204,14 @@ func dataSourceApiManagementRead(d *schema.ResourceData, meta interface{}) error
}
}

if err := d.Set("sku", flattenDataSourceApiManagementServiceSku(resp.Sku)); err != nil {
return fmt.Errorf("Error setting `sku`: %+v", err)
if sku := resp.Sku; sku != nil {
// TODO: Remove in 2.0
if err := d.Set("sku", flattenApiManagementServiceSku(resp.Sku)); err != nil {
return fmt.Errorf("Error setting `sku`: %+v", err)
}
if err := d.Set("sku_name", flattenApiManagementServiceSkuName(resp.Sku)); err != nil {
return fmt.Errorf("Error setting `sku_name`: %+v", err)
}
}

return tags.FlattenAndSet(d, resp.Tags)
Expand Down Expand Up @@ -288,22 +299,6 @@ func flattenDataSourceApiManagementAdditionalLocations(input *[]apimanagement.Ad
return results
}

func flattenDataSourceApiManagementServiceSku(profile *apimanagement.ServiceSkuProperties) []interface{} {
if profile == nil {
return []interface{}{}
}

sku := make(map[string]interface{})

sku["name"] = string(profile.Name)

if profile.Capacity != nil {
sku["capacity"] = *profile.Capacity
}

return []interface{}{sku}
}

func apiManagementDataSourceHostnameSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"host_name": {
Expand Down
58 changes: 58 additions & 0 deletions azurerm/helpers/azure/sku.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package azure

import (
"fmt"
"strconv"
"strings"

"github.com/hashicorp/terraform/helper/schema"
)

func SplitSku(sku string) (string, int32, error) {
skuParts := strings.Split(sku, "_")

if len(skuParts) != 2 {
return "", -1, fmt.Errorf("sku_name(%s) is not formatted properly.", sku)
}

capacity, err := strconv.Atoi(skuParts[1])

if err != nil {
return "", -1, fmt.Errorf("%s in sku_name is not a valid value.", skuParts[1])
}

return skuParts[0], int32(capacity), nil
}

// MinCapacitySkuNameInSlice returns a SchemaValidateFunc which tests if the provided value
// is of type string and matches the value of an element in the valid slice
// will test with in lower case if ignoreCase is true will also validate if the
// capacity if above passed minCapacity value
func MinCapacitySkuNameInSlice(valid []string, minCapacity int32, ignoreCase bool) schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}

name, capacity, err := SplitSku(v)

if err != nil {
es = append(es, err)
return
}

for _, str := range valid {
if name == str || (ignoreCase && strings.ToLower(name) == strings.ToLower(str)) {
if capacity < minCapacity {
es = append(es, fmt.Errorf("expected %s capacity value to be greater that %d, got %d", k, minCapacity, capacity))
}
return
}
}

es = append(es, fmt.Errorf("expected %s to be one of %v, got %s", k, valid, name))
return
}
}
101 changes: 85 additions & 16 deletions azurerm/resource_arm_api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ func resourceArmApiManagementService() *schema.Resource {
ValidateFunc: validate.ApiManagementServicePublisherEmail,
},

// TODO: Remove in 2.0
"sku": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
Computed: true,
Deprecated: "This property has been deprecated in favour of the 'sku_name' property and will be removed in version 2.0 of the provider",
ConflictsWith: []string{"sku_name"},
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -80,15 +84,29 @@ func resourceArmApiManagementService() *schema.Resource {
string(apimanagement.SkuTypePremium),
}, false),
},

"capacity": {
Type: schema.TypeInt,
Required: true,
Optional: true,
ValidateFunc: validation.IntAtLeast(0),
},
},
},
},

"sku_name": {
Type: schema.TypeString,
Optional: true,
Computed: true, // TODO: Remove computed in 2.0
ConflictsWith: []string{"sku"},
ValidateFunc: azure.MinCapacitySkuNameInSlice([]string{
string(apimanagement.SkuTypeDeveloper),
string(apimanagement.SkuTypeBasic),
string(apimanagement.SkuTypeStandard),
string(apimanagement.SkuTypePremium),
}, 1, false),
},

"identity": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -176,7 +194,7 @@ func resourceArmApiManagementService() *schema.Resource {
"security": {
Type: schema.TypeList,
Optional: true,
Computed: true, // todo remove in 2.0 ?
Computed: true, // TODO: Remove in 2.0 ?
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -198,15 +216,15 @@ func resourceArmApiManagementService() *schema.Resource {
"disable_triple_des_chipers": {
Type: schema.TypeBool,
Optional: true,
Computed: true, // todo remove in 2.0
Computed: true, // TODO: Remove in 2.0
Deprecated: "This field has been deprecated in favour of the `disable_triple_des_ciphers` property to correct the spelling. it will be removed in version 2.0 of the provider",
ConflictsWith: []string{"security.0.disable_triple_des_ciphers"},
},
"disable_triple_des_ciphers": {
Type: schema.TypeBool,
Optional: true,
// Default: false, // todo remove in 2.0
Computed: true, // todo remove in 2.0
// Default: false, // TODO: Remove in 2.0
Computed: true, // TODO: Remove in 2.0
ConflictsWith: []string{"security.0.disable_triple_des_chipers"},
},
"disable_frontend_ssl30": {
Expand Down Expand Up @@ -378,6 +396,14 @@ func resourceArmApiManagementServiceCreateUpdate(d *schema.ResourceData, meta in
client := meta.(*ArmClient).apiManagement.ServiceClient
ctx := meta.(*ArmClient).StopContext

// TODO: Remove in 2.0
sku := expandAzureRmApiManagementSku(d)
if sku == nil {
if sku = expandAzureRmApiManagementSkuName(d); sku == nil {
return fmt.Errorf("either 'sku_name' or 'sku' must be defined in the configuration file")
}
}

log.Printf("[INFO] preparing arguments for API Management Service creation.")

name := d.Get("name").(string)
Expand All @@ -399,8 +425,6 @@ func resourceArmApiManagementServiceCreateUpdate(d *schema.ResourceData, meta in
location := azure.NormalizeLocation(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})

sku := expandAzureRmApiManagementSku(d)

publisherName := d.Get("publisher_name").(string)
publisherEmail := d.Get("publisher_email").(string)
notificationSenderEmail := d.Get("notification_sender_email").(string)
Expand Down Expand Up @@ -574,8 +598,14 @@ func resourceArmApiManagementServiceRead(d *schema.ResourceData, meta interface{
}
}

if err := d.Set("sku", flattenApiManagementServiceSku(resp.Sku)); err != nil {
return fmt.Errorf("Error setting `sku`: %+v", err)
if sku := resp.Sku; sku != nil {
// TODO: Remove in 2.0
if err := d.Set("sku", flattenApiManagementServiceSku(resp.Sku)); err != nil {
return fmt.Errorf("Error setting `sku`: %+v", err)
}
if err := d.Set("sku_name", flattenApiManagementServiceSkuName(resp.Sku)); err != nil {
return fmt.Errorf("Error setting `sku_name`: %+v", err)
}
}

if err := d.Set("sign_in", flattenApiManagementSignInSettings(signInSettings)); err != nil {
Expand Down Expand Up @@ -859,22 +889,61 @@ func flattenAzureRmApiManagementMachineIdentity(identity *apimanagement.ServiceI
return []interface{}{result}
}

// TODO: Remove in 2.0 timeframe
func expandAzureRmApiManagementSku(d *schema.ResourceData) *apimanagement.ServiceSkuProperties {
var name string
var capacity int32

vs := d.Get("sku").([]interface{})

if len(vs) == 0 {
return nil
}

// guaranteed by MinItems in the schema
v := vs[0].(map[string]interface{})

name := apimanagement.SkuType(v["name"].(string))
capacity := int32(v["capacity"].(int))
name = v["name"].(string)
capacity = int32(v["capacity"].(int))

sku := &apimanagement.ServiceSkuProperties{
Name: apimanagement.SkuType(name),
Capacity: utils.Int32(capacity),
}

return sku
}

func expandAzureRmApiManagementSkuName(d *schema.ResourceData) *apimanagement.ServiceSkuProperties {
vs := d.Get("sku_name").(string)

if len(vs) == 0 {
return nil
}

name, capacity, err := azure.SplitSku(vs)
if err != nil {
return nil
}

sku := &apimanagement.ServiceSkuProperties{
Name: name,
Name: apimanagement.SkuType(name),
Capacity: utils.Int32(capacity),
}

return sku
}

func flattenApiManagementServiceSkuName(input *apimanagement.ServiceSkuProperties) string {
if input == nil {
return ""
}

sku := fmt.Sprintf("%s_%d", string(input.Name), *input.Capacity)

return sku
}

func flattenApiManagementServiceSku(input *apimanagement.ServiceSkuProperties) []interface{} {
if input == nil {
return []interface{}{}
Expand Down Expand Up @@ -939,7 +1008,7 @@ func flattenApiManagementCustomProperties(input map[string]*string) []interface{
output["disable_frontend_ssl30"] = parseApiManagementNilableDictionary(input, apimFrontendProtocolSsl3)
output["disable_frontend_tls10"] = parseApiManagementNilableDictionary(input, apimFrontendProtocolTls10)
output["disable_frontend_tls11"] = parseApiManagementNilableDictionary(input, apimFrontendProtocolTls11)
output["disable_triple_des_chipers"] = parseApiManagementNilableDictionary(input, apimTripleDesCiphers) // todo remove in 2.0
output["disable_triple_des_chipers"] = parseApiManagementNilableDictionary(input, apimTripleDesCiphers) // TODO: Remove in 2.0
output["disable_triple_des_ciphers"] = parseApiManagementNilableDictionary(input, apimTripleDesCiphers)

return []interface{}{output}
Expand Down
Loading

0 comments on commit a1d0ce1

Please sign in to comment.