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

128 lines
3.3 KiB
Go
Raw Normal View History

2018-09-04 11:37:12 +00:00
package digitalocean
import (
"context"
"fmt"
"net/http"
2018-09-04 11:37:12 +00:00
"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
// When the certificate type is lets_encrypt, the certificate
// ID will change when it's renewed, so we have to rely on the
// certificate name as the primary identifier instead.
// We include the UUID as another computed field for use in the
// short-term refresh function that waits for it to be ready.
"uuid": {
Type: schema.TypeString,
Computed: true,
Description: "uuid of the certificate",
},
2018-09-04 11:37:12 +00:00
"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
// When the certificate type is lets_encrypt, the certificate
// ID will change when it's renewed, so we have to rely on the
// certificate name as the primary identifier instead.
2018-09-04 11:37:12 +00:00
name := d.Get("name").(string)
cert, err := findCertificateByName(client, name)
if err != nil {
return err
}
2018-09-04 11:37:12 +00:00
d.SetId(cert.Name)
d.Set("name", cert.Name)
d.Set("uuid", cert.ID)
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(client *godo.Client, name string) (*godo.Certificate, error) {
2018-09-04 11:37:12 +00:00
opts := &godo.ListOptions{
Page: 1,
PerPage: 200,
}
for {
certs, resp, err := client.Certificates.List(context.Background(), opts)
if resp != nil && resp.StatusCode == http.StatusNotFound {
return nil, nil
}
2018-09-04 11:37:12 +00:00
if err != nil {
return nil, fmt.Errorf("Error retrieving certificates: %s", err)
2018-09-04 11:37:12 +00:00
}
for _, cert := range certs {
if cert.Name == name {
return &cert, nil
}
2018-09-04 11:37:12 +00:00
}
if resp.Links == nil || resp.Links.IsLastPage() {
break
}
page, err := resp.Links.CurrentPage()
if err != nil {
return nil, fmt.Errorf("Error retrieving certificates: %s", err)
2018-09-04 11:37:12 +00:00
}
opts.Page = page + 1
}
return nil, fmt.Errorf("Certificate %s not found", name)
2018-09-04 11:37:12 +00:00
}