|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | +) |
| 8 | + |
| 9 | +func dataSourcePrivatecaCertificateAuthority() *schema.Resource { |
| 10 | + dsSchema := datasourceSchemaFromResourceSchema(resourcePrivatecaCertificateAuthority().Schema) |
| 11 | + addOptionalFieldsToSchema(dsSchema, "project") |
| 12 | + addOptionalFieldsToSchema(dsSchema, "location") |
| 13 | + addOptionalFieldsToSchema(dsSchema, "pool") |
| 14 | + addOptionalFieldsToSchema(dsSchema, "certificate_authority_id") |
| 15 | + |
| 16 | + dsSchema["pem_csr"] = &schema.Schema{ |
| 17 | + Type: schema.TypeString, |
| 18 | + Computed: true, |
| 19 | + } |
| 20 | + |
| 21 | + return &schema.Resource{ |
| 22 | + Read: dataSourcePrivatecaCertificateAuthorityRead, |
| 23 | + Schema: dsSchema, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func dataSourcePrivatecaCertificateAuthorityRead(d *schema.ResourceData, meta interface{}) error { |
| 28 | + config := meta.(*Config) |
| 29 | + userAgent, err := generateUserAgentString(d, config.userAgent) |
| 30 | + if err != nil { |
| 31 | + return fmt.Errorf("Error generating user agent: %s", err) |
| 32 | + } |
| 33 | + |
| 34 | + id, err := replaceVars(d, config, "projects/{{project}}/locations/{{location}}/caPools/{{pool}}/certificateAuthorities/{{certificate_authority_id}}") |
| 35 | + if err != nil { |
| 36 | + return fmt.Errorf("Error constructing id: %s", err) |
| 37 | + } |
| 38 | + |
| 39 | + d.SetId(id) |
| 40 | + |
| 41 | + err = resourcePrivatecaCertificateAuthorityRead(d, meta) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + // pem_csr is only applicable for SUBORDINATE CertificateAuthorities |
| 47 | + if d.Get("type") == "SUBORDINATE" { |
| 48 | + url, err := replaceVars(d, config, "{{PrivatecaBasePath}}projects/{{project}}/locations/{{location}}/caPools/{{pool}}/certificateAuthorities/{{certificate_authority_id}}:fetch") |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + billingProject := "" |
| 54 | + |
| 55 | + project, err := getProject(d, config) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("Error fetching project for CertificateAuthority: %s", err) |
| 58 | + } |
| 59 | + billingProject = project |
| 60 | + |
| 61 | + // err == nil indicates that the billing_project value was found |
| 62 | + if bp, err := getBillingProject(d, config); err == nil { |
| 63 | + billingProject = bp |
| 64 | + } |
| 65 | + |
| 66 | + res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil) |
| 67 | + if err != nil { |
| 68 | + return handleNotFoundError(err, d, fmt.Sprintf("PrivatecaCertificateAuthority %q", d.Id())) |
| 69 | + } |
| 70 | + if err := d.Set("pem_csr", res["pemCsr"]); err != nil { |
| 71 | + return fmt.Errorf("Error fetching CertificateAuthority: %s", err) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
0 commit comments