terraform/builtin/providers/aws/resource_aws_iam_saml_provi...

131 lines
3.2 KiB
Go
Raw Normal View History

package aws
import (
"fmt"
provider/aws: Refresh iam saml provider from state on 404 (#12602) Fixes: #12599 Before this patch: ``` % terraform plan Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. aws_iam_saml_provider.salesforce: Refreshing state... (ID: arn:aws:i...rce-test) Error refreshing state: 1 error(s) occurred: * aws_iam_saml_provider.salesforce: aws_iam_saml_provider.salesforce: NoSuchEntity: Manifest not found for arn arn:aws:iam::187416307283:saml-provider/tf-salesforce-test status code: 404, request id: fc32c7f8-0631-11e7-8e1f-29a8c10edf64 ``` After this patch: ``` % terraform plan ✚ ✭ [WARN] /Users/stacko/Code/go/bin/terraform-provider-aws overrides an internal plugin for aws-provider. If you did not expect to see this message you will need to remove the old plugin. See https://www.terraform.io/docs/internals/internal-plugins.html Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. aws_iam_saml_provider.salesforce: Refreshing state... (ID: arn:aws:i...rce-test) The Terraform execution plan has been generated and is shown below. Resources are shown in alphabetical order for quick scanning. Green resources will be created (or destroyed and then created if an existing resource exists), yellow resources are being changed in-place, and red resources will be destroyed. Cyan entries are data sources to be read. Note: You didn't specify an "-out" parameter to save this plan, so when "apply" is called, Terraform can't guarantee this is what will execute. + aws_iam_saml_provider.salesforce arn: "<computed>" name: "tf-salesforce-test" ```
2017-03-13 09:18:29 +01:00
"log"
"regexp"
"time"
"github.com/aws/aws-sdk-go/aws"
provider/aws: Refresh iam saml provider from state on 404 (#12602) Fixes: #12599 Before this patch: ``` % terraform plan Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. aws_iam_saml_provider.salesforce: Refreshing state... (ID: arn:aws:i...rce-test) Error refreshing state: 1 error(s) occurred: * aws_iam_saml_provider.salesforce: aws_iam_saml_provider.salesforce: NoSuchEntity: Manifest not found for arn arn:aws:iam::187416307283:saml-provider/tf-salesforce-test status code: 404, request id: fc32c7f8-0631-11e7-8e1f-29a8c10edf64 ``` After this patch: ``` % terraform plan ✚ ✭ [WARN] /Users/stacko/Code/go/bin/terraform-provider-aws overrides an internal plugin for aws-provider. If you did not expect to see this message you will need to remove the old plugin. See https://www.terraform.io/docs/internals/internal-plugins.html Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. aws_iam_saml_provider.salesforce: Refreshing state... (ID: arn:aws:i...rce-test) The Terraform execution plan has been generated and is shown below. Resources are shown in alphabetical order for quick scanning. Green resources will be created (or destroyed and then created if an existing resource exists), yellow resources are being changed in-place, and red resources will be destroyed. Cyan entries are data sources to be read. Note: You didn't specify an "-out" parameter to save this plan, so when "apply" is called, Terraform can't guarantee this is what will execute. + aws_iam_saml_provider.salesforce arn: "<computed>" name: "tf-salesforce-test" ```
2017-03-13 09:18:29 +01:00
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsIamSamlProvider() *schema.Resource {
return &schema.Resource{
Create: resourceAwsIamSamlProviderCreate,
Read: resourceAwsIamSamlProviderRead,
Update: resourceAwsIamSamlProviderUpdate,
Delete: resourceAwsIamSamlProviderDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"valid_until": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"saml_metadata_document": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceAwsIamSamlProviderCreate(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn
input := &iam.CreateSAMLProviderInput{
Name: aws.String(d.Get("name").(string)),
SAMLMetadataDocument: aws.String(d.Get("saml_metadata_document").(string)),
}
out, err := iamconn.CreateSAMLProvider(input)
if err != nil {
return err
}
d.SetId(*out.SAMLProviderArn)
return resourceAwsIamSamlProviderRead(d, meta)
}
func resourceAwsIamSamlProviderRead(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn
input := &iam.GetSAMLProviderInput{
SAMLProviderArn: aws.String(d.Id()),
}
out, err := iamconn.GetSAMLProvider(input)
if err != nil {
provider/aws: Refresh iam saml provider from state on 404 (#12602) Fixes: #12599 Before this patch: ``` % terraform plan Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. aws_iam_saml_provider.salesforce: Refreshing state... (ID: arn:aws:i...rce-test) Error refreshing state: 1 error(s) occurred: * aws_iam_saml_provider.salesforce: aws_iam_saml_provider.salesforce: NoSuchEntity: Manifest not found for arn arn:aws:iam::187416307283:saml-provider/tf-salesforce-test status code: 404, request id: fc32c7f8-0631-11e7-8e1f-29a8c10edf64 ``` After this patch: ``` % terraform plan ✚ ✭ [WARN] /Users/stacko/Code/go/bin/terraform-provider-aws overrides an internal plugin for aws-provider. If you did not expect to see this message you will need to remove the old plugin. See https://www.terraform.io/docs/internals/internal-plugins.html Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. aws_iam_saml_provider.salesforce: Refreshing state... (ID: arn:aws:i...rce-test) The Terraform execution plan has been generated and is shown below. Resources are shown in alphabetical order for quick scanning. Green resources will be created (or destroyed and then created if an existing resource exists), yellow resources are being changed in-place, and red resources will be destroyed. Cyan entries are data sources to be read. Note: You didn't specify an "-out" parameter to save this plan, so when "apply" is called, Terraform can't guarantee this is what will execute. + aws_iam_saml_provider.salesforce arn: "<computed>" name: "tf-salesforce-test" ```
2017-03-13 09:18:29 +01:00
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
log.Printf("[WARN] IAM SAML Provider %q not found.", d.Id())
d.SetId("")
return nil
}
return err
}
validUntil := out.ValidUntil.Format(time.RFC1123)
d.Set("arn", d.Id())
name, err := extractNameFromIAMSamlProviderArn(d.Id(), meta.(*AWSClient).partition)
if err != nil {
return err
}
d.Set("name", name)
d.Set("valid_until", validUntil)
d.Set("saml_metadata_document", *out.SAMLMetadataDocument)
return nil
}
func resourceAwsIamSamlProviderUpdate(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn
input := &iam.UpdateSAMLProviderInput{
SAMLProviderArn: aws.String(d.Id()),
SAMLMetadataDocument: aws.String(d.Get("saml_metadata_document").(string)),
}
_, err := iamconn.UpdateSAMLProvider(input)
if err != nil {
return err
}
return resourceAwsIamSamlProviderRead(d, meta)
}
func resourceAwsIamSamlProviderDelete(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn
input := &iam.DeleteSAMLProviderInput{
SAMLProviderArn: aws.String(d.Id()),
}
_, err := iamconn.DeleteSAMLProvider(input)
return err
}
func extractNameFromIAMSamlProviderArn(arn, partition string) (string, error) {
// arn:aws:iam::123456789012:saml-provider/tf-salesforce-test
r := regexp.MustCompile(fmt.Sprintf("^arn:%s:iam::[0-9]{12}:saml-provider/(.+)$", partition))
submatches := r.FindStringSubmatch(arn)
if len(submatches) != 2 {
return "", fmt.Errorf("Unable to extract name from a given ARN: %q", arn)
}
return submatches[1], nil
}