terraform-provider-greenhost/digitalocean/datasource_digitalocean_cer...

124 lines
2.9 KiB
Go
Raw Normal View History

2018-09-04 11:37:12 +00:00
package digitalocean
import (
"context"
"fmt"
"github.com/digitalocean/godo"
Upgrade to terraform-plugin-sdk (#321) * Upgrade to terraform-plugin-sdk After following the [guide](https://www.terraform.io/docs/extend/plugin-sdk.html#using-tf-sdk-migrator) Check ``` ▶ GO111MODULE=on tf-sdk-migrator check Checking Go runtime version ... Go version 1.12.10: OK. Checking whether provider uses Go modules... Go modules in use: OK. Checking version of github.com/hashicorp/terraform-plugin-sdk to determine if provider was already migrated... Checking version of github.com/hashicorp/terraform used in provider... Terraform version 0.12.8: OK. Checking whether provider uses deprecated SDK packages or identifiers... No imports of deprecated SDK packages or identifiers: OK. All constraints satisfied. Provider can be migrated to the new SDK. ``` Migrate ``` ▶ GO111MODULE=on tf-sdk-migrator migrate Checking Go runtime version ... Go version 1.12.10: OK. Checking whether provider uses Go modules... Go modules in use: OK. Checking version of github.com/hashicorp/terraform-plugin-sdk to determine if provider was already migrated... Checking version of github.com/hashicorp/terraform used in provider... Terraform version 0.12.8: OK. Checking whether provider uses deprecated SDK packages or identifiers... No imports of deprecated SDK packages or identifiers: OK. All constraints satisfied. Provider can be migrated to the new SDK. Rewriting provider go.mod file... Rewriting SDK package imports... Running `go mod tidy`... Success! Provider is migrated to github.com/hashicorp/terraform-plugin-sdk v1.1.0. It looks like this provider vendors dependencies. Don't forget to run `go mod vendor`. Make sure to review all changes and run all tests. ``` * Fix build under go 1.13.x.
2019-10-22 21:44:03 +00:00
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
2018-09-04 11:37:12 +00:00
)
func dataSourceDigitalOceanCertificate() *schema.Resource {
return &schema.Resource{
Read: dataSourceDigitalOceanCertificateRead,
Schema: map[string]*schema.Schema{
"name": {
2018-09-04 11:37:12 +00:00
Type: schema.TypeString,
Required: true,
Description: "name of the certificate",
ValidateFunc: validation.NoZeroValues,
},
// computed attributes
"type": {
Type: schema.TypeString,
Computed: true,
Description: "type of the certificate",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "current state of the certificate",
},
"domains": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "domains for which the certificate was issued",
},
"not_after": {
Type: schema.TypeString,
Computed: true,
Description: "expiration date and time of the certificate",
},
"sha1_fingerprint": {
Type: schema.TypeString,
Computed: true,
Description: "SHA1 fingerprint of the certificate",
},
},
}
}
func dataSourceDigitalOceanCertificateRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
2018-09-04 11:37:12 +00:00
name := d.Get("name").(string)
opts := &godo.ListOptions{
Page: 1,
PerPage: 200,
}
certList := []godo.Certificate{}
for {
certs, resp, err := client.Certificates.List(context.Background(), opts)
if err != nil {
return fmt.Errorf("Error retrieving ssh keys: %s", err)
}
for _, cert := range certs {
certList = append(certList, cert)
}
if resp.Links == nil || resp.Links.IsLastPage() {
break
}
page, err := resp.Links.CurrentPage()
if err != nil {
return fmt.Errorf("Error retrieving ssh keys: %s", err)
}
opts.Page = page + 1
}
cert, err := findCertificateByName(certList, name)
if err != nil {
return err
}
d.SetId(cert.ID)
d.Set("name", cert.Name)
d.Set("type", cert.Type)
d.Set("state", cert.State)
d.Set("not_after", cert.NotAfter)
d.Set("sha1_fingerprint", cert.SHA1Fingerprint)
if err := d.Set("domains", flattenDigitalOceanCertificateDomains(cert.DNSNames)); err != nil {
return fmt.Errorf("Error setting `domain`: %+v", err)
}
return nil
}
func findCertificateByName(certs []godo.Certificate, name string) (*godo.Certificate, error) {
results := make([]godo.Certificate, 0)
for _, v := range certs {
if v.Name == name {
results = append(results, v)
}
}
if len(results) == 1 {
return &results[0], nil
}
if len(results) == 0 {
return nil, fmt.Errorf("no certificate found with name %s", name)
}
return nil, fmt.Errorf("too many certificate found with name %s (found %d, expected 1)", name, len(results))
}