2015-09-02 15:44:12 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2016-07-12 18:04:01 +02:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2015-09-02 15:44:12 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"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,
|
|
|
|
|
2016-07-12 18:04:01 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
2015-09-02 15:44:12 +02:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"valid_until": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"saml_metadata_document": &schema.Schema{
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
validUntil := out.ValidUntil.Format(time.RFC1123)
|
2015-11-09 17:07:31 +01:00
|
|
|
d.Set("arn", d.Id())
|
2016-07-12 18:04:01 +02:00
|
|
|
name, err := extractNameFromIAMSamlProviderArn(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Set("name", name)
|
2015-09-02 15:44:12 +02:00
|
|
|
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
|
|
|
|
}
|
2016-07-12 18:04:01 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|