Skip to content

Commit

Permalink
Merge branch 'master' into 012/early4
Browse files Browse the repository at this point in the history
  • Loading branch information
katbyte committed Feb 28, 2019
2 parents 8d0a4da + 17389c7 commit 92a9dd6
Show file tree
Hide file tree
Showing 16 changed files with 1,653 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ FEATURES:
* **New Data Source:** `azurerm_network_watcher` [GH-2791]
* **New Data Source:** `azurerm_recovery_services_protection_policy_vm` [GH-2974]
* **New Resource:** `azurerm_api_management_group` [GH-2809]
* **New Resource:** `azurerm_api_management_group_user` [GH-2972]
* **New Resource:** `azurerm_api_management_product` [GH-2953]
* **New Resource:** `azurerm_api_management_user` [GH-2954]
* **New Resource:** `azurerm_eventgrid_domain` [GH-2884]
* **New Resource:** `azurerm_eventgrid_event_subscription` [GH-2967]
* **New Resource:** `azurerm_lb_outbound_rule` [GH-2912]
* **New Resource:** `azurerm_media_service_account` [GH-2711]
* **New Resource:** `azurerm_network_watcher` [GH-2791]
Expand Down
28 changes: 19 additions & 9 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,12 @@ type ArmClient struct {
kubernetesClustersClient containerservice.ManagedClustersClient
containerGroupsClient containerinstance.ContainerGroupsClient

eventGridDomainsClient eventgrid.DomainsClient
eventGridTopicsClient eventgrid.TopicsClient
eventHubClient eventhub.EventHubsClient
eventHubConsumerGroupClient eventhub.ConsumerGroupsClient
eventHubNamespacesClient eventhub.NamespacesClient
eventGridDomainsClient eventgrid.DomainsClient
eventGridEventSubscriptionsClient eventgrid.EventSubscriptionsClient
eventGridTopicsClient eventgrid.TopicsClient
eventHubClient eventhub.EventHubsClient
eventHubConsumerGroupClient eventhub.ConsumerGroupsClient
eventHubNamespacesClient eventhub.NamespacesClient

solutionsClient operationsmanagement.SolutionsClient

Expand All @@ -126,10 +127,11 @@ type ArmClient struct {
redisPatchSchedulesClient redis.PatchSchedulesClient

// API Management
apiManagementGroupClient apimanagement.GroupClient
apiManagementProductsClient apimanagement.ProductClient
apiManagementServiceClient apimanagement.ServiceClient
apiManagementUsersClient apimanagement.UserClient
apiManagementGroupClient apimanagement.GroupClient
apiManagementGroupUsersClient apimanagement.GroupUserClient
apiManagementProductsClient apimanagement.ProductClient
apiManagementServiceClient apimanagement.ServiceClient
apiManagementUsersClient apimanagement.UserClient

// Application Insights
appInsightsClient appinsights.ComponentsClient
Expand Down Expand Up @@ -494,6 +496,10 @@ func (c *ArmClient) registerApiManagementServiceClients(endpoint, subscriptionId
c.configureClient(&groupsClient.Client, auth)
c.apiManagementGroupClient = groupsClient

groupUsersClient := apimanagement.NewGroupUserClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&groupUsersClient.Client, auth)
c.apiManagementGroupUsersClient = groupUsersClient

serviceClient := apimanagement.NewServiceClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&serviceClient.Client, auth)
c.apiManagementServiceClient = serviceClient
Expand Down Expand Up @@ -862,6 +868,10 @@ func (c *ArmClient) registerEventGridClients(endpoint, subscriptionId string, au
egdc := eventgrid.NewDomainsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&egdc.Client, auth)
c.eventGridDomainsClient = egdc

egesc := eventgrid.NewEventSubscriptionsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&egesc.Client, auth)
c.eventGridEventSubscriptionsClient = egesc
}

func (c *ArmClient) registerEventHubClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
Expand Down
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"azurerm_api_management": resourceArmApiManagementService(),
"azurerm_api_management_group": resourceArmApiManagementGroup(),
"azurerm_api_management_group_user": resourceArmApiManagementGroupUser(),
"azurerm_api_management_product": resourceArmApiManagementProduct(),
"azurerm_api_management_user": resourceArmApiManagementUser(),
"azurerm_app_service_active_slot": resourceArmAppServiceActiveSlot(),
Expand Down Expand Up @@ -226,6 +227,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_dns_txt_record": resourceArmDnsTxtRecord(),
"azurerm_dns_zone": resourceArmDnsZone(),
"azurerm_eventgrid_domain": resourceArmEventGridDomain(),
"azurerm_eventgrid_event_subscription": resourceArmEventGridEventSubscription(),
"azurerm_eventgrid_topic": resourceArmEventGridTopic(),
"azurerm_eventhub_authorization_rule": resourceArmEventHubAuthorizationRule(),
"azurerm_eventhub_consumer_group": resourceArmEventHubConsumerGroup(),
Expand Down
14 changes: 14 additions & 0 deletions azurerm/resource_arm_api_management_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"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/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -73,6 +74,19 @@ func resourceArmApiManagementGroupCreateUpdate(d *schema.ResourceData, meta inte
externalID := d.Get("external_id").(string)
groupType := d.Get("type").(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 Group %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_group", *existing.ID)
}
}

parameters := apimanagement.GroupCreateParameters{
GroupCreateParametersProperties: &apimanagement.GroupCreateParametersProperties{
DisplayName: utils.String(displayName),
Expand Down
45 changes: 45 additions & 0 deletions azurerm/resource_arm_api_management_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ func TestAccAzureRMAPIManagementGroup_basic(t *testing.T) {
})
}

func TestAccAzureRMAPIManagementGroup_requiresImport(t *testing.T) {
if !requireResourcesToBeImported {
t.Skip("Skipping since resources aren't required to be imported")
return
}

resourceName := "azurerm_api_management_group.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAPIManagementGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAPIManagementGroup_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAPIManagementGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "display_name", "Test Group"),
resource.TestCheckResourceAttr(resourceName, "type", "custom"),
),
},
{
Config: testAccAzureRMAPIManagementGroup_requiresImport(ri, location),
ExpectError: testRequiresImportError("azurerm_api_management_group"),
},
},
})
}

func TestAccAzureRMAPIManagementGroup_complete(t *testing.T) {
resourceName := "azurerm_api_management_group.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -186,6 +217,20 @@ resource "azurerm_api_management_group" "test" {
`, rInt, location, rInt, rInt)
}

func testAccAzureRMAPIManagementGroup_requiresImport(rInt int, location string) string {
template := testAccAzureRMAPIManagementGroup_basic(rInt, location)
return fmt.Sprintf(`
%s
resource "azurerm_api_management_group" "import" {
name = "${azurerm_api_management_group.test.name}"
resource_group_name = "${azurerm_api_management_group.test.resource_group_name}"
api_management_name = "${azurerm_api_management_group.test.api_management_name}"
display_name = "${azurerm_api_management_group.test.display_name}"
}
`, template)
}

func testAccAzureRMAPIManagementGroup_complete(rInt int, location string, displayName, description string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
121 changes: 121 additions & 0 deletions azurerm/resource_arm_api_management_group_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
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/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmApiManagementGroupUser() *schema.Resource {
return &schema.Resource{
Create: resourceArmApiManagementGroupUserCreate,
Read: resourceArmApiManagementGroupUserRead,
Delete: resourceArmApiManagementGroupUserDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

"group_name": azure.SchemaApiManagementChildName(),

"resource_group_name": resourceGroupNameSchema(),

"api_management_name": azure.SchemaApiManagementName(),
},
}
}

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

resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
groupName := d.Get("group_name").(string)
userId := d.Get("user_id").(string)

if requireResourcesToBeImported {
resp, err := client.CheckEntityExists(ctx, resourceGroup, serviceName, groupName, userId)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error checking for present of existing User %q / Group %q (API Management Service %q / Resource Group %q): %+v", userId, groupName, serviceName, resourceGroup, err)
}
}

if !utils.ResponseWasNotFound(resp) {
subscriptionId := meta.(*ArmClient).subscriptionId
resourceId := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ApiManagement/service/%s/groups/%s/users/%s", subscriptionId, resourceGroup, serviceName, groupName, userId)
return tf.ImportAsExistsError("azurerm_api_management_group_user", resourceId)
}
}

resp, err := client.Create(ctx, resourceGroup, serviceName, groupName, userId)
if err != nil {
return fmt.Errorf("Error adding User %q to Group %q (API Management Service %q / Resource Group %q): %+v", userId, groupName, serviceName, resourceGroup, err)
}

// there's no Read so this is best-effort
d.SetId(*resp.ID)

return resourceArmApiManagementGroupUserRead(d, meta)
}

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

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
groupName := id.Path["groups"]
userId := id.Path["users"]

resp, err := client.CheckEntityExists(ctx, resourceGroup, serviceName, groupName, userId)
if err != nil {
if utils.ResponseWasNotFound(resp) {
log.Printf("[DEBUG] User %q was not found in Group %q (API Management Service %q / Resource Group %q) was not found - removing from state!", userId, groupName, serviceName, resourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error retrieving User %q / Group %q (API Management Service %q / Resource Group %q): %+v", userId, groupName, serviceName, resourceGroup, err)
}

d.Set("group_name", groupName)
d.Set("user_id", userId)
d.Set("resource_group_name", resourceGroup)
d.Set("api_management_name", serviceName)

return nil
}

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

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
groupName := id.Path["groups"]
userId := id.Path["users"]

if resp, err := client.Delete(ctx, resourceGroup, serviceName, groupName, userId); err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error removing User %q from Group %q (API Management Service %q / Resource Group %q): %+v", userId, groupName, serviceName, resourceGroup, err)
}
}

return nil
}
Loading

0 comments on commit 92a9dd6

Please sign in to comment.