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_google #5279

Merged
merged 3 commits into from
Jan 2, 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
14 changes: 14 additions & 0 deletions azurerm/helpers/validate/google.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package validate

import (
"fmt"
"regexp"
)

func GoogleClientID(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[A-Za-z0-9-]+\.apps\.googleusercontent\.com$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%s must start with an identifier containing alphanumeric characters and hyphens and end with '.apps.googleusercontent.com'", k))
}
return warnings, errors
}
41 changes: 41 additions & 0 deletions azurerm/helpers/validate/google_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package validate

import (
"testing"
)

func TestGoogleClientID(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{
Value: "",
ErrCount: 1,
},
{
Value: "invalid client id",
ErrCount: 1,
},
{
Value: "00000000-0000-0000-0000-000000000000",
ErrCount: 1,
},
{
Value: "123456789000-abcd12ef34gh56ij78900klmnopqrst0.apps.googleusercontent.org",
ErrCount: 1,
},
{
Value: "123456789000-abcd12ef34gh56ij78900klmnopqrst0.apps.googleusercontent.com",
ErrCount: 0,
},
}

for _, tc := range cases {
_, errors := GoogleClientID(tc.Value, "client_id")

if len(errors) != tc.ErrCount {
t.Fatalf("Expected the Google Client ID %s to trigger a validation error", tc.Value)
}
}
}
47 changes: 24 additions & 23 deletions azurerm/internal/services/apimanagement/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,29 @@ 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_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_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 resourceArmApiManagementIdentityProviderGoogle() *schema.Resource {
return &schema.Resource{
Create: resourceArmApiManagementIdentityProviderGoogleCreateUpdate,
Read: resourceArmApiManagementIdentityProviderGoogleRead,
Update: resourceArmApiManagementIdentityProviderGoogleCreateUpdate,
Delete: resourceArmApiManagementIdentityProviderGoogleDelete,
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.GoogleClientID,
},

"client_secret": {
Copy link
Collaborator

@katbyte katbyte Dec 31, 2019

Choose a reason for hiding this comment

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

Could we change this to match what google calls this:

Suggested change
"client_secret": {
"api_key": {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe both Azure and Google actually refer to the value as client secret. I'll update the documentation to reflect this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you look at the SDK docs (and presumably the API) it states:

// ClientSecret - Client secret of the Application in external Identity Provider, used to authenticate login request. For example, it is App Secret for Facebook login, API Key for Google login, Public Key for Microsoft.

Is this inaccurate and client secret is what you would look for on the google side?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, it is called client secret as well on the Google side.

google

Copy link
Collaborator

Choose a reason for hiding this comment

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

perfect, thanks for that!

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

func resourceArmApiManagementIdentityProviderGoogleCreateUpdate(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)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, serviceName, apimanagement.Google)
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.Google, serviceName, resourceGroup, err)
}
}

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

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

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

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

return resourceArmApiManagementIdentityProviderGoogleRead(d, meta)
}

func resourceArmApiManagementIdentityProviderGoogleRead(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)
}

return nil
}

func resourceArmApiManagementIdentityProviderGoogleDelete(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