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_identity_provider_twitter #5306

Merged
merged 1 commit into from
Jan 8, 2020
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
49 changes: 25 additions & 24 deletions azurerm/internal/services/apimanagement/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,30 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
// SupportedResources returns the supported Resources supported by this Service
func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_api_management": resourceArmApiManagementService(),
"azurerm_api_management_api": resourceArmApiManagementApi(),
"azurerm_api_management_api_operation": resourceArmApiManagementApiOperation(),
"azurerm_api_management_api_operation_policy": resourceArmApiManagementApiOperationPolicy(),
"azurerm_api_management_api_policy": resourceArmApiManagementApiPolicy(),
"azurerm_api_management_api_schema": resourceArmApiManagementApiSchema(),
"azurerm_api_management_api_version_set": resourceArmApiManagementApiVersionSet(),
"azurerm_api_management_authorization_server": resourceArmApiManagementAuthorizationServer(),
"azurerm_api_management_backend": resourceArmApiManagementBackend(),
"azurerm_api_management_certificate": resourceArmApiManagementCertificate(),
"azurerm_api_management_diagnostic": resourceArmApiManagementDiagnostic(),
"azurerm_api_management_group": resourceArmApiManagementGroup(),
"azurerm_api_management_group_user": resourceArmApiManagementGroupUser(),
"azurerm_api_management_identity_provider_aad": resourceArmApiManagementIdentityProviderAAD(),
"azurerm_api_management_identity_provider_google": resourceArmApiManagementIdentityProviderGoogle(),
"azurerm_api_management_logger": resourceArmApiManagementLogger(),
"azurerm_api_management_openid_connect_provider": resourceArmApiManagementOpenIDConnectProvider(),
"azurerm_api_management_product": resourceArmApiManagementProduct(),
"azurerm_api_management_product_api": resourceArmApiManagementProductApi(),
"azurerm_api_management_product_group": resourceArmApiManagementProductGroup(),
"azurerm_api_management_product_policy": resourceArmApiManagementProductPolicy(),
"azurerm_api_management_property": resourceArmApiManagementProperty(),
"azurerm_api_management_subscription": resourceArmApiManagementSubscription(),
"azurerm_api_management_user": resourceArmApiManagementUser(),
"azurerm_api_management": resourceArmApiManagementService(),
"azurerm_api_management_api": resourceArmApiManagementApi(),
"azurerm_api_management_api_operation": resourceArmApiManagementApiOperation(),
"azurerm_api_management_api_operation_policy": resourceArmApiManagementApiOperationPolicy(),
"azurerm_api_management_api_policy": resourceArmApiManagementApiPolicy(),
"azurerm_api_management_api_schema": resourceArmApiManagementApiSchema(),
"azurerm_api_management_api_version_set": resourceArmApiManagementApiVersionSet(),
"azurerm_api_management_authorization_server": resourceArmApiManagementAuthorizationServer(),
"azurerm_api_management_backend": resourceArmApiManagementBackend(),
"azurerm_api_management_certificate": resourceArmApiManagementCertificate(),
"azurerm_api_management_diagnostic": resourceArmApiManagementDiagnostic(),
"azurerm_api_management_group": resourceArmApiManagementGroup(),
"azurerm_api_management_group_user": resourceArmApiManagementGroupUser(),
"azurerm_api_management_identity_provider_aad": resourceArmApiManagementIdentityProviderAAD(),
"azurerm_api_management_identity_provider_google": resourceArmApiManagementIdentityProviderGoogle(),
"azurerm_api_management_identity_provider_twitter": resourceArmApiManagementIdentityProviderTwitter(),
"azurerm_api_management_logger": resourceArmApiManagementLogger(),
"azurerm_api_management_openid_connect_provider": resourceArmApiManagementOpenIDConnectProvider(),
"azurerm_api_management_product": resourceArmApiManagementProduct(),
"azurerm_api_management_product_api": resourceArmApiManagementProductApi(),
"azurerm_api_management_product_group": resourceArmApiManagementProductGroup(),
"azurerm_api_management_product_policy": resourceArmApiManagementProductPolicy(),
"azurerm_api_management_property": resourceArmApiManagementProperty(),
"azurerm_api_management_subscription": resourceArmApiManagementSubscription(),
"azurerm_api_management_user": resourceArmApiManagementUser(),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
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 resourceArmApiManagementIdentityProviderTwitter() *schema.Resource {
return &schema.Resource{
Create: resourceArmApiManagementIdentityProviderTwitterCreateUpdate,
Read: resourceArmApiManagementIdentityProviderTwitterRead,
Update: resourceArmApiManagementIdentityProviderTwitterCreateUpdate,
Delete: resourceArmApiManagementIdentityProviderTwitterDelete,
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(),

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

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

func resourceArmApiManagementIdentityProviderTwitterCreateUpdate(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("api_key").(string)
clientSecret := d.Get("api_secret_key").(string)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, serviceName, apimanagement.Twitter)
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.Twitter, serviceName, resourceGroup, err)
katbyte marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

parameters := apimanagement.IdentityProviderContract{
IdentityProviderContractProperties: &apimanagement.IdentityProviderContractProperties{
ClientID: utils.String(clientID),
ClientSecret: utils.String(clientSecret),
Type: apimanagement.Twitter,
},
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, apimanagement.Twitter, parameters, ""); err != nil {
return fmt.Errorf("Error creating or updating Identity Provider %q (Resource Group %q / API Management Service %q): %+v", apimanagement.Twitter, resourceGroup, serviceName, err)
}

resp, err := client.Get(ctx, resourceGroup, serviceName, apimanagement.Twitter)
if err != nil {
return fmt.Errorf("Error retrieving Identity Provider %q (Resource Group %q / API Management Service %q): %+v", apimanagement.Twitter, 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.Twitter, resourceGroup, serviceName)
}
d.SetId(*resp.ID)

return resourceArmApiManagementIdentityProviderTwitterRead(d, meta)
}

func resourceArmApiManagementIdentityProviderTwitterRead(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("api_key", props.ClientID)
d.Set("api_secret_key", props.ClientSecret)
}

return nil
}

func resourceArmApiManagementIdentityProviderTwitterDelete(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
}
Loading