-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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 #8114 from gdlx/cognito-ui-customization
Cognito User Pool UI Customization
- Loading branch information
Showing
8 changed files
with
1,053 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
aws_cognito_user_pool_ui_customization | ||
``` |
35 changes: 35 additions & 0 deletions
35
aws/internal/service/cognitoidentityprovider/finder/finder.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,35 @@ | ||
package finder | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider" | ||
) | ||
|
||
// CognitoUserPoolUICustomization returns the UI Customization corresponding to the UserPoolId and ClientId. | ||
// Returns nil if no UI Customization is found. | ||
func CognitoUserPoolUICustomization(conn *cognitoidentityprovider.CognitoIdentityProvider, userPoolId, clientId string) (*cognitoidentityprovider.UICustomizationType, error) { | ||
input := &cognitoidentityprovider.GetUICustomizationInput{ | ||
ClientId: aws.String(clientId), | ||
UserPoolId: aws.String(userPoolId), | ||
} | ||
|
||
output, err := conn.GetUICustomization(input) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if output == nil || output.UICustomization == nil { | ||
return nil, nil | ||
} | ||
|
||
// The GetUICustomization API operation will return an empty struct | ||
// if nothing is present rather than nil or an error, so we equate that with nil | ||
if reflect.DeepEqual(output.UICustomization, &cognitoidentityprovider.UICustomizationType{}) { | ||
return nil, nil | ||
} | ||
|
||
return output.UICustomization, 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
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,187 @@ | ||
package aws | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider" | ||
"github.com/hashicorp/aws-sdk-go-base/tfawserr" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/cognitoidentityprovider/finder" | ||
) | ||
|
||
func resourceAwsCognitoUserPoolUICustomization() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsCognitoUserPoolUICustomizationPut, | ||
Read: resourceAwsCognitoUserPoolUICustomizationRead, | ||
Update: resourceAwsCognitoUserPoolUICustomizationPut, | ||
Delete: resourceAwsCognitoUserPoolUICustomizationDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"client_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "ALL", | ||
}, | ||
|
||
"creation_date": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"css": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
AtLeastOneOf: []string{"css", "image_file"}, | ||
}, | ||
|
||
"css_version": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"image_file": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
AtLeastOneOf: []string{"image_file", "css"}, | ||
}, | ||
|
||
"image_url": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"last_modified_date": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"user_pool_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsCognitoUserPoolUICustomizationPut(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).cognitoidpconn | ||
|
||
clientId := d.Get("client_id").(string) | ||
userPoolId := d.Get("user_pool_id").(string) | ||
|
||
input := &cognitoidentityprovider.SetUICustomizationInput{ | ||
ClientId: aws.String(clientId), | ||
UserPoolId: aws.String(userPoolId), | ||
} | ||
|
||
if v, ok := d.GetOk("css"); ok { | ||
input.CSS = aws.String(v.(string)) | ||
} | ||
|
||
if v, ok := d.GetOk("image_file"); ok { | ||
imgFile, err := base64.StdEncoding.DecodeString(v.(string)) | ||
if err != nil { | ||
return fmt.Errorf("error Base64 decoding image file for Cognito User Pool UI customization (UserPoolId: %s, ClientId: %s): %w", userPoolId, clientId, err) | ||
} | ||
|
||
input.ImageFile = imgFile | ||
} | ||
|
||
_, err := conn.SetUICustomization(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error setting Cognito User Pool UI customization (UserPoolId: %s, ClientId: %s): %w", userPoolId, clientId, err) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s,%s", userPoolId, clientId)) | ||
|
||
return resourceAwsCognitoUserPoolUICustomizationRead(d, meta) | ||
} | ||
|
||
func resourceAwsCognitoUserPoolUICustomizationRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).cognitoidpconn | ||
|
||
userPoolId, clientId, err := parseCognitoUserPoolUICustomizationID(d.Id()) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error parsing Cognito User Pool UI customization ID (%s): %w", d.Id(), err) | ||
} | ||
|
||
uiCustomization, err := finder.CognitoUserPoolUICustomization(conn, userPoolId, clientId) | ||
|
||
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, cognitoidentityprovider.ErrCodeResourceNotFoundException) { | ||
log.Printf("[WARN] Cognito User Pool UI customization (UserPoolId: %s, ClientId: %s) not found, removing from state", userPoolId, clientId) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error getting Cognito User Pool UI customization (UserPoolId: %s, ClientId: %s): %w", userPoolId, clientId, err) | ||
} | ||
|
||
if uiCustomization == nil { | ||
if d.IsNewResource() { | ||
return fmt.Errorf("error getting Cognito User Pool UI customization (UserPoolId: %s, ClientId: %s): not found", userPoolId, clientId) | ||
} | ||
|
||
log.Printf("[WARN] Cognito User Pool UI customization (UserPoolId: %s, ClientId: %s) not found, removing from state", userPoolId, clientId) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.Set("client_id", uiCustomization.ClientId) | ||
d.Set("creation_date", aws.TimeValue(uiCustomization.CreationDate).Format(time.RFC3339)) | ||
d.Set("css", uiCustomization.CSS) | ||
d.Set("css_version", uiCustomization.CSSVersion) | ||
d.Set("image_url", uiCustomization.ImageUrl) | ||
d.Set("last_modified_date", aws.TimeValue(uiCustomization.LastModifiedDate).Format(time.RFC3339)) | ||
d.Set("user_pool_id", uiCustomization.UserPoolId) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsCognitoUserPoolUICustomizationDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).cognitoidpconn | ||
|
||
userPoolId, clientId, err := parseCognitoUserPoolUICustomizationID(d.Id()) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error parsing Cognito User Pool UI customization ID (%s): %w", d.Id(), err) | ||
} | ||
|
||
input := &cognitoidentityprovider.SetUICustomizationInput{ | ||
ClientId: aws.String(clientId), | ||
UserPoolId: aws.String(userPoolId), | ||
} | ||
|
||
_, err = conn.SetUICustomization(input) | ||
|
||
if tfawserr.ErrCodeEquals(err, cognitoidentityprovider.ErrCodeResourceNotFoundException) { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error deleting Cognito User Pool UI customization (UserPoolId: %s, ClientId: %s): %w", userPoolId, clientId, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func parseCognitoUserPoolUICustomizationID(id string) (string, string, error) { | ||
idParts := strings.SplitN(id, ",", 2) | ||
|
||
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { | ||
return "", "", fmt.Errorf("please make sure ID is in format USER_POOL_ID,CLIENT_ID") | ||
} | ||
|
||
return idParts[0], idParts[1], nil | ||
} |
Oops, something went wrong.