|
| 1 | +package certificatemanager |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 8 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 9 | + "google.golang.org/api/certificatemanager/v1" |
| 10 | +) |
| 11 | + |
| 12 | +func DataSourceGoogleCertificateManagerCertificates() *schema.Resource { |
| 13 | + dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceCertificateManagerCertificate().Schema) |
| 14 | + tpgresource.DeleteFieldsFromSchema(dsSchema, "self_managed") |
| 15 | + |
| 16 | + return &schema.Resource{ |
| 17 | + Read: dataSourceGoogleCertificateManagerCertificatesRead, |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "filter": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Optional: true, |
| 22 | + }, |
| 23 | + "region": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + Default: "global", |
| 27 | + }, |
| 28 | + "certificates": { |
| 29 | + Type: schema.TypeList, |
| 30 | + Computed: true, |
| 31 | + Elem: &schema.Resource{ |
| 32 | + Schema: dsSchema, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func dataSourceGoogleCertificateManagerCertificatesRead(d *schema.ResourceData, meta interface{}) error { |
| 40 | + config := meta.(*transport_tpg.Config) |
| 41 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + project, err := tpgresource.GetProject(d, config) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("error fetching project for certificate: %s", err) |
| 49 | + } |
| 50 | + |
| 51 | + region, err := tpgresource.GetRegion(d, config) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("error fetching region for certificate: %s", err) |
| 54 | + } |
| 55 | + |
| 56 | + filter := d.Get("filter").(string) |
| 57 | + |
| 58 | + certificates := make([]map[string]interface{}, 0) |
| 59 | + certificatesList, err := config.NewCertificateManagerClient(userAgent).Projects.Locations.Certificates.List(fmt.Sprintf("projects/%s/locations/%s", project, region)).Filter(filter).Do() |
| 60 | + if err != nil { |
| 61 | + return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Certificates : %s %s", project, region)) |
| 62 | + } |
| 63 | + |
| 64 | + for _, certificate := range certificatesList.Certificates { |
| 65 | + if certificate != nil { |
| 66 | + certificates = append(certificates, map[string]interface{}{ |
| 67 | + "name": certificate.Name, |
| 68 | + "description": certificate.Description, |
| 69 | + "labels": certificate.Labels, |
| 70 | + "location": region, |
| 71 | + "managed": flattenCertificateManaged(certificate.Managed), |
| 72 | + "san_dnsnames": certificate.SanDnsnames, |
| 73 | + "scope": certificate.Scope, |
| 74 | + }) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if err := d.Set("certificates", certificates); err != nil { |
| 79 | + return fmt.Errorf("error setting certificates: %s", err) |
| 80 | + } |
| 81 | + |
| 82 | + d.SetId(fmt.Sprintf( |
| 83 | + "projects/%s/locations/%s/certificates", |
| 84 | + project, |
| 85 | + region, |
| 86 | + )) |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func flattenCertificateManaged(v *certificatemanager.ManagedCertificate) interface{} { |
| 92 | + if v == nil { |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + output := make(map[string]interface{}) |
| 97 | + |
| 98 | + output["authorization_attempt_info"] = flattenCertificateManagedAuthorizationAttemptInfo(v.AuthorizationAttemptInfo) |
| 99 | + output["dns_authorizations"] = v.DnsAuthorizations |
| 100 | + output["domains"] = v.Domains |
| 101 | + output["issuance_config"] = v.IssuanceConfig |
| 102 | + output["state"] = v.State |
| 103 | + output["provisioning_issue"] = flattenCertificateManagedProvisioningIssue(v.ProvisioningIssue) |
| 104 | + |
| 105 | + return []interface{}{output} |
| 106 | +} |
| 107 | + |
| 108 | +func flattenCertificateManagedAuthorizationAttemptInfo(v []*certificatemanager.AuthorizationAttemptInfo) interface{} { |
| 109 | + if v == nil { |
| 110 | + return nil |
| 111 | + } |
| 112 | + |
| 113 | + output := make([]interface{}, 0, len(v)) |
| 114 | + |
| 115 | + for _, authorizationAttemptInfo := range v { |
| 116 | + output = append(output, map[string]interface{}{ |
| 117 | + "details": authorizationAttemptInfo.Details, |
| 118 | + "domain": authorizationAttemptInfo.Domain, |
| 119 | + "failure_reason": authorizationAttemptInfo.FailureReason, |
| 120 | + "state": authorizationAttemptInfo.State, |
| 121 | + }) |
| 122 | + } |
| 123 | + |
| 124 | + return output |
| 125 | +} |
| 126 | + |
| 127 | +func flattenCertificateManagedProvisioningIssue(v *certificatemanager.ProvisioningIssue) interface{} { |
| 128 | + if v == nil { |
| 129 | + return nil |
| 130 | + } |
| 131 | + |
| 132 | + output := make(map[string]interface{}) |
| 133 | + |
| 134 | + output["details"] = v.Details |
| 135 | + output["reason"] = v.Reason |
| 136 | + |
| 137 | + return []interface{}{output} |
| 138 | +} |
0 commit comments