provider/aws: Allow importing iam_saml_provider (#7605)

This commit is contained in:
Radek Simko 2016-07-12 17:04:01 +01:00 committed by Paul Stack
parent 1ad0402b29
commit df40b91fb8
2 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package aws
import (
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccAWSIAMSamlProvider_importBasic(t *testing.T) {
resourceName := "aws_iam_saml_provider.salesforce"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckIAMSamlProviderDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccIAMSamlProviderConfig,
},
resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

View File

@ -1,6 +1,8 @@
package aws package aws
import ( import (
"fmt"
"regexp"
"time" "time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
@ -16,6 +18,10 @@ func resourceAwsIamSamlProvider() *schema.Resource {
Update: resourceAwsIamSamlProviderUpdate, Update: resourceAwsIamSamlProviderUpdate,
Delete: resourceAwsIamSamlProviderDelete, Delete: resourceAwsIamSamlProviderDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"arn": &schema.Schema{ "arn": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
@ -69,6 +75,11 @@ func resourceAwsIamSamlProviderRead(d *schema.ResourceData, meta interface{}) er
validUntil := out.ValidUntil.Format(time.RFC1123) validUntil := out.ValidUntil.Format(time.RFC1123)
d.Set("arn", d.Id()) d.Set("arn", d.Id())
name, err := extractNameFromIAMSamlProviderArn(d.Id())
if err != nil {
return err
}
d.Set("name", name)
d.Set("valid_until", validUntil) d.Set("valid_until", validUntil)
d.Set("saml_metadata_document", *out.SAMLMetadataDocument) d.Set("saml_metadata_document", *out.SAMLMetadataDocument)
@ -100,3 +111,13 @@ func resourceAwsIamSamlProviderDelete(d *schema.ResourceData, meta interface{})
return err return err
} }
func extractNameFromIAMSamlProviderArn(arn string) (string, error) {
// arn:aws:iam::123456789012:saml-provider/tf-salesforce-test
r := regexp.MustCompile("^arn:aws:iam::[0-9]{12}:saml-provider/(.+)$")
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
}