github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_iam_saml_provider.go (about)

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