Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource: azurerm_api_management_api_version_set #3073

Merged
merged 11 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ type ArmClient struct {
// API Management
apiManagementApiClient apimanagement.APIClient
apiManagementApiOperationsClient apimanagement.APIOperationClient
apiManagementApiVersionSetClient apimanagement.APIVersionSetClient
apiManagementAuthorizationServersClient apimanagement.AuthorizationServerClient
apiManagementCertificatesClient apimanagement.CertificateClient
apiManagementGroupClient apimanagement.GroupClient
Expand Down Expand Up @@ -510,6 +511,10 @@ func (c *ArmClient) registerApiManagementServiceClients(endpoint, subscriptionId
c.configureClient(&apiOperationsClient.Client, auth)
c.apiManagementApiOperationsClient = apiOperationsClient

apiVersionSetClient := apimanagement.NewAPIVersionSetClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&apiVersionSetClient.Client, auth)
c.apiManagementApiVersionSetClient = apiVersionSetClient

authorizationServersClient := apimanagement.NewAuthorizationServerClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&authorizationServersClient.Client, auth)
c.apiManagementAuthorizationServersClient = authorizationServersClient
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_api_management": resourceArmApiManagementService(),
"azurerm_api_management_api": resourceArmApiManagementApi(),
"azurerm_api_management_api_operation": resourceArmApiManagementApiOperation(),
"azurerm_api_management_api_version_set": resourceArmApiManagementApiVersionSet(),
"azurerm_api_management_authorization_server": resourceArmApiManagementAuthorizationServer(),
"azurerm_api_management_certificate": resourceArmApiManagementCertificate(),
"azurerm_api_management_group": resourceArmApiManagementGroup(),
Expand Down
212 changes: 212 additions & 0 deletions azurerm/resource_arm_api_management_api_version_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package azurerm

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/validation"

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2018-01-01/apimanagement"
"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/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmApiManagementApiVersionSet() *schema.Resource {
return &schema.Resource{
Create: resourceArmApiManagementApiVersionSetCreateUpdate,
Read: resourceArmApiManagementApiVersionSetRead,
Update: resourceArmApiManagementApiVersionSetCreateUpdate,
Delete: resourceArmApiManagementApiVersionSetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": azure.SchemaApiManagementChildName(),

"resource_group_name": resourceGroupNameSchema(),

"api_management_name": azure.SchemaApiManagementName(),

"display_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"versioning_scheme": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(apimanagement.VersioningSchemeHeader),
string(apimanagement.VersioningSchemeQuery),
string(apimanagement.VersioningSchemeSegment),
}, false),
},

"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.NoEmptyStrings,
metacpp marked this conversation as resolved.
Show resolved Hide resolved
},

"version_header_name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.NoEmptyStrings,
ConflictsWith: []string{"version_query_name"},
},

"version_query_name": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need the version prefix here, so this can become querystring_name

Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.NoEmptyStrings,
ConflictsWith: []string{"version_header_name"},
},
},
}
}

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, serviceName, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Api Version Set %q (Api Management Service %q / Resource Group %q): %s", name, serviceName, resourceGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_api_management_api_version_set", *existing.ID)
}
}

versioningScheme := apimanagement.VersioningScheme(d.Get("versioning_scheme").(string))
parameters := apimanagement.APIVersionSetContract{
APIVersionSetContractProperties: &apimanagement.APIVersionSetContractProperties{
DisplayName: utils.String(d.Get("display_name").(string)),
VersioningScheme: versioningScheme,
Description: utils.String(d.Get("description").(string)),
},
}

var headerSet, querySet bool
if v, ok := d.GetOk("version_header_name"); ok {
headerSet = v.(string) != ""
parameters.APIVersionSetContractProperties.VersionHeaderName = utils.String(v.(string))
}
if v, ok := d.GetOk("version_query_name"); ok {
querySet = v.(string) != ""
parameters.APIVersionSetContractProperties.VersionQueryName = utils.String(v.(string))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defining these in this manner means it's not possible to overwrite these fields (e.g. if switching from Header -> Query) - instead we can just assign them normally as below

Suggested change
}
headerName := d.Get("header_name").(string)
queryStringName := d.Get("querystring_name").(string)

Copy link
Contributor Author

@metacpp metacpp Apr 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You changed the behavior from nil pointer to pointer to empty string.


switch schema := versioningScheme; schema {
case apimanagement.VersioningSchemeHeader:
if !headerSet {
return fmt.Errorf("`version_header_name` must be set if `versioning_schema` is `Header`")
}
if querySet {
return fmt.Errorf("`version_query_name` can not be set if `versioning_schema` is `Header`")
}

case apimanagement.VersioningSchemeQuery:
if headerSet {
return fmt.Errorf("`version_header_name` can not be set if `versioning_schema` is `Query`")
}
if !querySet {
return fmt.Errorf("`version_query_name` must be set if `versioning_schema` is `Query`")
}

case apimanagement.VersioningSchemeSegment:
if headerSet {
return fmt.Errorf("`version_header_name` can not be set if `versioning_schema` is `Segment`")
}
if querySet {
return fmt.Errorf("`version_query_name` can not be set if `versioning_schema` is `Segment`")
}
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, name, parameters, ""); err != nil {
return fmt.Errorf("Error creating/updating Api Version Set %q (Resource Group %q / Api Management Service %q): %+v", name, resourceGroup, serviceName, err)
}

resp, err := client.Get(ctx, resourceGroup, serviceName, name)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor since we're not using err below we can combine these two lines:

Suggested change
if err != nil {
if resp, err := client.Get(ctx, resourceGroup, serviceName, name); err != nil {

return fmt.Errorf("Error retrieving Api Version Set %q (Resource Group %q / Api Management Service %q): %+v", name, resourceGroup, serviceName, err)
}
if resp.ID == nil {
return fmt.Errorf("Cannot read ID for Api Version Set %q (Resource Group %q / Api Management Service %q)", name, resourceGroup, serviceName)
}
d.SetId(*resp.ID)

return resourceArmApiManagementApiVersionSetRead(d, meta)
}

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

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
name := id.Path["api-version-sets"]

resp, err := client.Get(ctx, resourceGroup, serviceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Api Version Set %q (Resource Group %q / Api Management Service %q) was not found - removing from state!", name, resourceGroup, serviceName)
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request for Api Version Set %q (Resource Group %q / Api Management Service %q): %+v", name, resourceGroup, serviceName, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("api_management_name", serviceName)

if props := resp.APIVersionSetContractProperties; props != nil {
d.Set("description", props.Description)
d.Set("display_name", props.DisplayName)
d.Set("versioning_scheme", string(props.VersioningScheme))
d.Set("version_header_name", props.VersionHeaderName)
d.Set("version_query_name", props.VersionQueryName)
}

return nil
}

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

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
name := id.Path["api-version-sets"]

if resp, err := client.Delete(ctx, resourceGroup, serviceName, name, ""); err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error deleting Api Version Set %q (Resource Group %q / Api Management Service %q): %+v", name, resourceGroup, serviceName, err)
}
}

return nil
}
Loading