github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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  			"id": {
    62  				Type:     schema.TypeString,
    63  				Computed: true,
    64  			},
    65  
    66  			"path": {
    67  				Type:     schema.TypeString,
    68  				Computed: true,
    69  			},
    70  
    71  			"expiration_date": {
    72  				Type:     schema.TypeString,
    73  				Computed: true,
    74  			},
    75  		},
    76  	}
    77  }
    78  
    79  type certificateByExpiration []*iam.ServerCertificateMetadata
    80  
    81  func (m certificateByExpiration) Len() int {
    82  	return len(m)
    83  }
    84  
    85  func (m certificateByExpiration) Swap(i, j int) {
    86  	m[i], m[j] = m[j], m[i]
    87  }
    88  
    89  func (m certificateByExpiration) Less(i, j int) bool {
    90  	return m[i].Expiration.After(*m[j].Expiration)
    91  }
    92  
    93  func dataSourceAwsIAMServerCertificateRead(d *schema.ResourceData, meta interface{}) error {
    94  	iamconn := meta.(*AWSClient).iamconn
    95  
    96  	var matcher = func(cert *iam.ServerCertificateMetadata) bool {
    97  		return strings.HasPrefix(aws.StringValue(cert.ServerCertificateName), d.Get("name_prefix").(string))
    98  	}
    99  	if v, ok := d.GetOk("name"); ok {
   100  		matcher = func(cert *iam.ServerCertificateMetadata) bool {
   101  			return aws.StringValue(cert.ServerCertificateName) == v.(string)
   102  		}
   103  	}
   104  
   105  	var metadatas = []*iam.ServerCertificateMetadata{}
   106  	err := iamconn.ListServerCertificatesPages(&iam.ListServerCertificatesInput{}, func(p *iam.ListServerCertificatesOutput, lastPage bool) bool {
   107  		for _, cert := range p.ServerCertificateMetadataList {
   108  			if matcher(cert) {
   109  				metadatas = append(metadatas, cert)
   110  			}
   111  		}
   112  		return true
   113  	})
   114  	if err != nil {
   115  		return errwrap.Wrapf("Error describing certificates: {{err}}", err)
   116  	}
   117  
   118  	if len(metadatas) == 0 {
   119  		return fmt.Errorf("Search for AWS IAM server certificate returned no results")
   120  	}
   121  	if len(metadatas) > 1 {
   122  		if !d.Get("latest").(bool) {
   123  			return fmt.Errorf("Search for AWS IAM server certificate returned too many results")
   124  		}
   125  
   126  		sort.Sort(certificateByExpiration(metadatas))
   127  	}
   128  
   129  	metadata := metadatas[0]
   130  	d.SetId(*metadata.ServerCertificateId)
   131  	d.Set("arn", *metadata.Arn)
   132  	d.Set("path", *metadata.Path)
   133  	d.Set("id", *metadata.ServerCertificateId)
   134  	d.Set("name", *metadata.ServerCertificateName)
   135  	if metadata.Expiration != nil {
   136  		d.Set("expiration_date", metadata.Expiration.Format("2006-01-02T15:04:05"))
   137  	}
   138  
   139  	return nil
   140  }