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