github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/data_source_aws_iam_server_certificate.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/iam"
    10  	"github.com/hashicorp/errwrap"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func dataSourceAwsIAMServerCertificate() *schema.Resource {
    15  	return &schema.Resource{
    16  		Read: dataSourceAwsIAMServerCertificateRead,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": {
    20  				Type:          schema.TypeString,
    21  				Optional:      true,
    22  				Computed:      true,
    23  				ForceNew:      true,
    24  				ConflictsWith: []string{"name_prefix"},
    25  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    26  					value := v.(string)
    27  					if len(value) > 128 {
    28  						errors = append(errors, fmt.Errorf(
    29  							"%q cannot be longer than 128 characters", k))
    30  					}
    31  					return
    32  				},
    33  			},
    34  
    35  			"name_prefix": {
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  				ForceNew: true,
    39  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    40  					value := v.(string)
    41  					if len(value) > 30 {
    42  						errors = append(errors, fmt.Errorf(
    43  							"%q cannot be longer than 30 characters, name is limited to 128", k))
    44  					}
    45  					return
    46  				},
    47  			},
    48  
    49  			"latest": {
    50  				Type:     schema.TypeBool,
    51  				Optional: true,
    52  				ForceNew: true,
    53  				Default:  false,
    54  			},
    55  
    56  			"arn": {
    57  				Type:     schema.TypeString,
    58  				Computed: true,
    59  			},
    60  
    61  			"path": {
    62  				Type:     schema.TypeString,
    63  				Computed: true,
    64  			},
    65  
    66  			"expiration_date": {
    67  				Type:     schema.TypeString,
    68  				Computed: true,
    69  			},
    70  		},
    71  	}
    72  }
    73  
    74  type certificateByExpiration []*iam.ServerCertificateMetadata
    75  
    76  func (m certificateByExpiration) Len() int {
    77  	return len(m)
    78  }
    79  
    80  func (m certificateByExpiration) Swap(i, j int) {
    81  	m[i], m[j] = m[j], m[i]
    82  }
    83  
    84  func (m certificateByExpiration) Less(i, j int) bool {
    85  	return m[i].Expiration.After(*m[j].Expiration)
    86  }
    87  
    88  func dataSourceAwsIAMServerCertificateRead(d *schema.ResourceData, meta interface{}) error {
    89  	iamconn := meta.(*AWSClient).iamconn
    90  
    91  	var matcher = func(cert *iam.ServerCertificateMetadata) bool {
    92  		return strings.HasPrefix(aws.StringValue(cert.ServerCertificateName), d.Get("name_prefix").(string))
    93  	}
    94  	if v, ok := d.GetOk("name"); ok {
    95  		matcher = func(cert *iam.ServerCertificateMetadata) bool {
    96  			return aws.StringValue(cert.ServerCertificateName) == v.(string)
    97  		}
    98  	}
    99  
   100  	var metadatas = []*iam.ServerCertificateMetadata{}
   101  	err := iamconn.ListServerCertificatesPages(&iam.ListServerCertificatesInput{}, func(p *iam.ListServerCertificatesOutput, lastPage bool) bool {
   102  		for _, cert := range p.ServerCertificateMetadataList {
   103  			if matcher(cert) {
   104  				metadatas = append(metadatas, cert)
   105  			}
   106  		}
   107  		return true
   108  	})
   109  	if err != nil {
   110  		return errwrap.Wrapf("Error describing certificates: {{err}}", err)
   111  	}
   112  
   113  	if len(metadatas) == 0 {
   114  		return fmt.Errorf("Search for AWS IAM server certificate returned no results")
   115  	}
   116  	if len(metadatas) > 1 {
   117  		if !d.Get("latest").(bool) {
   118  			return fmt.Errorf("Search for AWS IAM server certificate returned too many results")
   119  		}
   120  
   121  		sort.Sort(certificateByExpiration(metadatas))
   122  	}
   123  
   124  	metadata := metadatas[0]
   125  	d.SetId(*metadata.ServerCertificateId)
   126  	d.Set("arn", *metadata.Arn)
   127  	d.Set("path", *metadata.Path)
   128  	d.Set("name", *metadata.ServerCertificateName)
   129  	if metadata.Expiration != nil {
   130  		d.Set("expiration_date", metadata.Expiration.Format("2006-01-02T15:04:05"))
   131  	}
   132  
   133  	return nil
   134  }