-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2809 from terraform-providers/resource_apimnggroup
New Resource: azurerm_api_management_group
- Loading branch information
Showing
13 changed files
with
667 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceApiManagementGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceApiManagementGroupRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": azure.SchemaApiManagementChildDataSourceName(), | ||
|
||
"resource_group_name": resourceGroupNameForDataSourceSchema(), | ||
|
||
"api_management_name": azure.SchemaApiManagementDataSourceName(), | ||
|
||
"display_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"external_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceApiManagementGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).apiManagementGroupClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
resourceGroup := d.Get("resource_group_name").(string) | ||
serviceName := d.Get("api_management_name").(string) | ||
name := d.Get("name").(string) | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serviceName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[DEBUG] Group %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 Group %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("resource_group_name", resourceGroup) | ||
d.Set("api_management_name", serviceName) | ||
|
||
if properties := resp.GroupContractProperties; properties != nil { | ||
d.Set("display_name", properties.DisplayName) | ||
d.Set("description", properties.Description) | ||
d.Set("external_id", properties.ExternalID) | ||
d.Set("type", string(properties.Type)) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
) | ||
|
||
func TestAccDataSourceAzureRMApiManagementGroup_basic(t *testing.T) { | ||
dataSourceName := "data.azurerm_api_management_group.test" | ||
rInt := tf.AccRandTimeInt() | ||
location := testLocation() | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceApiManagementGroup_basic(rInt, location), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataSourceName, "display_name", "Test Group"), | ||
resource.TestCheckResourceAttr(dataSourceName, "description", ""), | ||
resource.TestCheckResourceAttr(dataSourceName, "external_id", ""), | ||
resource.TestCheckResourceAttr(dataSourceName, "type", "custom"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceApiManagementGroup_basic(rInt int, location string) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_api_management" "test" { | ||
name = "acctestAM-%d" | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
publisher_name = "pub1" | ||
publisher_email = "[email protected]" | ||
sku { | ||
name = "Developer" | ||
capacity = 1 | ||
} | ||
} | ||
resource "azurerm_api_management_group" "test" { | ||
name = "acctestAMGroup-%d" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
api_management_name = "${azurerm_api_management.test.name}" | ||
display_name = "Test Group" | ||
} | ||
data "azurerm_api_management_group" "test" { | ||
name = "${azurerm_api_management_group.test.name}" | ||
api_management_name = "${azurerm_api_management_group.test.api_management_name}" | ||
resource_group_name = "${azurerm_api_management_group.test.resource_group_name}" | ||
} | ||
`, rInt, location, rInt, rInt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2018-01-01/apimanagement" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
"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/utils" | ||
) | ||
|
||
func resourceArmApiManagementGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmApiManagementGroupCreateUpdate, | ||
Read: resourceArmApiManagementGroupRead, | ||
Update: resourceArmApiManagementGroupCreateUpdate, | ||
Delete: resourceArmApiManagementGroupDelete, | ||
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, | ||
}, | ||
|
||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
|
||
"external_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: string(apimanagement.Custom), | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
string(apimanagement.Custom), | ||
string(apimanagement.External), | ||
}, false), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmApiManagementGroupCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).apiManagementGroupClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
serviceName := d.Get("api_management_name").(string) | ||
|
||
displayName := d.Get("display_name").(string) | ||
description := d.Get("description").(string) | ||
externalID := d.Get("external_id").(string) | ||
groupType := d.Get("type").(string) | ||
|
||
parameters := apimanagement.GroupCreateParameters{ | ||
GroupCreateParametersProperties: &apimanagement.GroupCreateParametersProperties{ | ||
DisplayName: utils.String(displayName), | ||
Description: utils.String(description), | ||
ExternalID: utils.String(externalID), | ||
Type: apimanagement.GroupType(groupType), | ||
}, | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, name, parameters, ""); err != nil { | ||
return fmt.Errorf("Error creating or updating Group %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err) | ||
} | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serviceName, name) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving Group %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err) | ||
} | ||
if resp.ID == nil { | ||
return fmt.Errorf("Cannot read ID for Group %q (Resource Group %q / API Management Service %q)", name, resourceGroup, serviceName) | ||
} | ||
d.SetId(*resp.ID) | ||
|
||
return resourceArmApiManagementGroupRead(d, meta) | ||
} | ||
|
||
func resourceArmApiManagementGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).apiManagementGroupClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
serviceName := id.Path["service"] | ||
name := id.Path["groups"] | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serviceName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[DEBUG] Group %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 Group %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 properties := resp.GroupContractProperties; properties != nil { | ||
d.Set("display_name", properties.DisplayName) | ||
d.Set("description", properties.Description) | ||
d.Set("external_id", properties.ExternalID) | ||
d.Set("type", string(properties.Type)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmApiManagementGroupDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).apiManagementGroupClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
serviceName := id.Path["service"] | ||
name := id.Path["groups"] | ||
|
||
if resp, err := client.Delete(ctx, resourceGroup, serviceName, name, ""); err != nil { | ||
if !utils.ResponseWasNotFound(resp) { | ||
return fmt.Errorf("Error deleting Group %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.