-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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_identity_provider_aad
#5268
Merged
katbyte
merged 3 commits into
hashicorp:master
from
aqche:resource_azurerm_api_management_identity_provider_aad
Dec 30, 2019
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
create azurerm_api_management_identity_provider_aad resource
commit 41f1bc34da25d331f4a6ad64cc20388a7076de91
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
170 changes: 170 additions & 0 deletions
170
azurerm/internal/services/apimanagement/resource_arm_api_management_identity_provider_aad.go
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,170 @@ | ||
package apimanagement | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2018-01-01/apimanagement" | ||
"github.com/hashicorp/terraform-plugin-sdk/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/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmApiManagementIdentityProviderAAD() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmApiManagementIdentityProviderAADCreateUpdate, | ||
Read: resourceArmApiManagementIdentityProviderAADRead, | ||
Update: resourceArmApiManagementIdentityProviderAADCreateUpdate, | ||
Delete: resourceArmApiManagementIdentityProviderAADDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Update: schema.DefaultTimeout(30 * time.Minute), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"api_management_name": azure.SchemaApiManagementName(), | ||
|
||
"client_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.NoEmptyStrings, | ||
}, | ||
|
||
"client_secret": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Sensitive: true, | ||
ValidateFunc: validate.NoEmptyStrings, | ||
}, | ||
|
||
"allowed_tenants": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
katbyte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmApiManagementIdentityProviderAADCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).ApiManagement.IdentityProviderClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
resourceGroup := d.Get("resource_group_name").(string) | ||
serviceName := d.Get("api_management_name").(string) | ||
clientID := d.Get("client_id").(string) | ||
clientSecret := d.Get("client_secret").(string) | ||
allowedTenants := d.Get("allowed_tenants").([]interface{}) | ||
|
||
if features.ShouldResourcesBeImported() && d.IsNewResource() { | ||
existing, err := client.Get(ctx, resourceGroup, serviceName, apimanagement.Aad) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("Error checking for presence of existing Identity Provider %q (API Management Service %q / Resource Group %q): %s", apimanagement.Aad, serviceName, resourceGroup, err) | ||
} | ||
} | ||
|
||
if existing.ID != nil && *existing.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_api_management_identity_provider_aad", *existing.ID) | ||
} | ||
} | ||
|
||
parameters := apimanagement.IdentityProviderContract{ | ||
IdentityProviderContractProperties: &apimanagement.IdentityProviderContractProperties{ | ||
ClientID: utils.String(clientID), | ||
ClientSecret: utils.String(clientSecret), | ||
Type: apimanagement.Aad, | ||
AllowedTenants: utils.ExpandStringSlice(allowedTenants), | ||
}, | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, apimanagement.Aad, parameters, ""); err != nil { | ||
return fmt.Errorf("Error creating or updating Identity Provider %q (Resource Group %q / API Management Service %q): %+v", apimanagement.Aad, resourceGroup, serviceName, err) | ||
} | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serviceName, apimanagement.Aad) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving Identity Provider %q (Resource Group %q / API Management Service %q): %+v", apimanagement.Aad, resourceGroup, serviceName, err) | ||
} | ||
if resp.ID == nil { | ||
return fmt.Errorf("Cannot read ID for Identity Provider %q (Resource Group %q / API Management Service %q)", apimanagement.Aad, resourceGroup, serviceName) | ||
} | ||
d.SetId(*resp.ID) | ||
|
||
return resourceArmApiManagementIdentityProviderAADRead(d, meta) | ||
} | ||
|
||
func resourceArmApiManagementIdentityProviderAADRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).ApiManagement.IdentityProviderClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := azure.ParseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
serviceName := id.Path["service"] | ||
identityProviderName := id.Path["identityProviders"] | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serviceName, apimanagement.IdentityProviderType(identityProviderName)) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[DEBUG] Identity Provider %q (Resource Group %q / API Management Service %q) was not found - removing from state!", identityProviderName, resourceGroup, serviceName) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error making Read request for Identity Provider %q (Resource Group %q / API Management Service %q): %+v", identityProviderName, resourceGroup, serviceName, err) | ||
} | ||
|
||
d.Set("resource_group_name", resourceGroup) | ||
d.Set("api_management_name", serviceName) | ||
|
||
if props := resp.IdentityProviderContractProperties; props != nil { | ||
d.Set("client_id", props.ClientID) | ||
d.Set("client_secret", props.ClientSecret) | ||
d.Set("allowed_tenants", props.AllowedTenants) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmApiManagementIdentityProviderAADDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).ApiManagement.IdentityProviderClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := azure.ParseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
serviceName := id.Path["service"] | ||
identityProviderName := id.Path["identityProviders"] | ||
|
||
if resp, err := client.Delete(ctx, resourceGroup, serviceName, apimanagement.IdentityProviderType(identityProviderName), ""); err != nil { | ||
if !utils.ResponseWasNotFound(resp) { | ||
return fmt.Errorf("Error deleting Identity Provider %q (Resource Group %q / API Management Service %q): %+v", identityProviderName, resourceGroup, serviceName, err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
146 changes: 146 additions & 0 deletions
146
...al/services/apimanagement/tests/resource_arm_api_management_identity_provider_aad_test.go
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,146 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2018-01-01/apimanagement" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func TestAccAzureRMApiManagementIdentityProviderAAD_basic(t *testing.T) { | ||
katbyte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data := acceptance.BuildTestData(t, "azurerm_api_management_identity_provider_aad", "test") | ||
config := testAccAzureRMApiManagementIdentityProviderAAD_basic(data) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
CheckDestroy: testCheckAzureRMApiManagementIdentityProviderAADDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMApiManagementIdentityProviderAADExists(data.ResourceName), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMApiManagementIdentityProviderAAD_requiresImport(t *testing.T) { | ||
if !features.ShouldResourcesBeImported() { | ||
t.Skip("Skipping since resources aren't required to be imported") | ||
return | ||
} | ||
data := acceptance.BuildTestData(t, "azurerm_api_management_identity_provider_aad", "test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
CheckDestroy: testCheckAzureRMApiManagementIdentityProviderAADDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAzureRMApiManagementIdentityProviderAAD_basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMApiManagementIdentityProviderAADExists(data.ResourceName), | ||
), | ||
}, | ||
data.RequiresImportErrorStep(testAccAzureRMApiManagementIdentityProviderAAD_requiresImport), | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAzureRMApiManagementIdentityProviderAADDestroy(s *terraform.State) error { | ||
client := acceptance.AzureProvider.Meta().(*clients.Client).ApiManagement.IdentityProviderClient | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "azurerm_api_management_identity_provider_aad" { | ||
continue | ||
} | ||
|
||
resourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
serviceName := rs.Primary.Attributes["api_management_name"] | ||
|
||
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext | ||
resp, err := client.Get(ctx, resourceGroup, serviceName, apimanagement.Aad) | ||
|
||
if err != nil { | ||
if !utils.ResponseWasNotFound(resp.Response) { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
return nil | ||
} | ||
|
||
func testCheckAzureRMApiManagementIdentityProviderAADExists(resourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", resourceName) | ||
} | ||
|
||
resourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
serviceName := rs.Primary.Attributes["api_management_name"] | ||
|
||
client := acceptance.AzureProvider.Meta().(*clients.Client).ApiManagement.IdentityProviderClient | ||
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext | ||
resp, err := client.Get(ctx, resourceGroup, serviceName, apimanagement.Aad) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Bad: API Management Identity Provider %q (Resource Group %q / API Management Service %q) does not exist", apimanagement.Aad, resourceGroup, serviceName) | ||
} | ||
return fmt.Errorf("Bad: Get on apiManagementIdentityProviderClient: %+v", err) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccAzureRMApiManagementIdentityProviderAAD_basic(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
aqche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 = "pub1@email.com" | ||
sku_name = "Developer_1" | ||
} | ||
|
||
resource "azurerm_api_management_identity_provider_aad" "test" { | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
api_management_name = "${azurerm_api_management.test.name}" | ||
client_id = "aadclientid" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think client IDs are guids?? |
||
client_secret = "aadsecret" | ||
allowed_tenants = ["%s"] | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.Client().TenantID) | ||
} | ||
|
||
func testAccAzureRMApiManagementIdentityProviderAAD_requiresImport(data acceptance.TestData) string { | ||
template := testAccAzureRMApiManagementIdentityProviderAAD_basic(data) | ||
return fmt.Sprintf(` | ||
%s | ||
|
||
resource "azurerm_api_management_identity_provider_aad" "import" { | ||
resource_group_name = "${azurerm_api_management_identity_provider_aad.test.resource_group_name}" | ||
api_management_name = "${azurerm_api_management_identity_provider_aad.test.api_management_name}" | ||
client_id = "${azurerm_api_management_identity_provider_aad.test.client_id}" | ||
client_secret = "${azurerm_api_management_identity_provider_aad.test.client_secret}" | ||
allowed_tenants = "${azurerm_api_management_identity_provider_aad.test.allowed_tenants}" | ||
} | ||
`, template) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we validate this is a guid (there is a existing function)