github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_iam_saml_provider.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "time" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/iam" 10 11 "github.com/hashicorp/terraform/helper/schema" 12 ) 13 14 func resourceAwsIamSamlProvider() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceAwsIamSamlProviderCreate, 17 Read: resourceAwsIamSamlProviderRead, 18 Update: resourceAwsIamSamlProviderUpdate, 19 Delete: resourceAwsIamSamlProviderDelete, 20 21 Importer: &schema.ResourceImporter{ 22 State: schema.ImportStatePassthrough, 23 }, 24 25 Schema: map[string]*schema.Schema{ 26 "arn": { 27 Type: schema.TypeString, 28 Computed: true, 29 }, 30 "valid_until": { 31 Type: schema.TypeString, 32 Computed: true, 33 }, 34 "name": { 35 Type: schema.TypeString, 36 Required: true, 37 ForceNew: true, 38 }, 39 "saml_metadata_document": { 40 Type: schema.TypeString, 41 Required: true, 42 }, 43 }, 44 } 45 } 46 47 func resourceAwsIamSamlProviderCreate(d *schema.ResourceData, meta interface{}) error { 48 iamconn := meta.(*AWSClient).iamconn 49 50 input := &iam.CreateSAMLProviderInput{ 51 Name: aws.String(d.Get("name").(string)), 52 SAMLMetadataDocument: aws.String(d.Get("saml_metadata_document").(string)), 53 } 54 55 out, err := iamconn.CreateSAMLProvider(input) 56 if err != nil { 57 return err 58 } 59 60 d.SetId(*out.SAMLProviderArn) 61 62 return resourceAwsIamSamlProviderRead(d, meta) 63 } 64 65 func resourceAwsIamSamlProviderRead(d *schema.ResourceData, meta interface{}) error { 66 iamconn := meta.(*AWSClient).iamconn 67 68 input := &iam.GetSAMLProviderInput{ 69 SAMLProviderArn: aws.String(d.Id()), 70 } 71 out, err := iamconn.GetSAMLProvider(input) 72 if err != nil { 73 return err 74 } 75 76 validUntil := out.ValidUntil.Format(time.RFC1123) 77 d.Set("arn", d.Id()) 78 name, err := extractNameFromIAMSamlProviderArn(d.Id(), meta.(*AWSClient).partition) 79 if err != nil { 80 return err 81 } 82 d.Set("name", name) 83 d.Set("valid_until", validUntil) 84 d.Set("saml_metadata_document", *out.SAMLMetadataDocument) 85 86 return nil 87 } 88 89 func resourceAwsIamSamlProviderUpdate(d *schema.ResourceData, meta interface{}) error { 90 iamconn := meta.(*AWSClient).iamconn 91 92 input := &iam.UpdateSAMLProviderInput{ 93 SAMLProviderArn: aws.String(d.Id()), 94 SAMLMetadataDocument: aws.String(d.Get("saml_metadata_document").(string)), 95 } 96 _, err := iamconn.UpdateSAMLProvider(input) 97 if err != nil { 98 return err 99 } 100 101 return resourceAwsIamSamlProviderRead(d, meta) 102 } 103 104 func resourceAwsIamSamlProviderDelete(d *schema.ResourceData, meta interface{}) error { 105 iamconn := meta.(*AWSClient).iamconn 106 107 input := &iam.DeleteSAMLProviderInput{ 108 SAMLProviderArn: aws.String(d.Id()), 109 } 110 _, err := iamconn.DeleteSAMLProvider(input) 111 112 return err 113 } 114 115 func extractNameFromIAMSamlProviderArn(arn, partition string) (string, error) { 116 // arn:aws:iam::123456789012:saml-provider/tf-salesforce-test 117 r := regexp.MustCompile(fmt.Sprintf("^arn:%s:iam::[0-9]{12}:saml-provider/(.+)$", partition)) 118 submatches := r.FindStringSubmatch(arn) 119 if len(submatches) != 2 { 120 return "", fmt.Errorf("Unable to extract name from a given ARN: %q", arn) 121 } 122 return submatches[1], nil 123 }