forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 5
/
data_source_certificate_authority.go
88 lines (72 loc) · 2.82 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
77
78
79
80
81
82
83
84
85
86
87
88
package privateca
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)
func DataSourcePrivatecaCertificateAuthority() *schema.Resource {
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourcePrivatecaCertificateAuthority().Schema)
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
tpgresource.AddOptionalFieldsToSchema(dsSchema, "location")
tpgresource.AddOptionalFieldsToSchema(dsSchema, "pool")
tpgresource.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.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return fmt.Errorf("Error generating user agent: %s", err)
}
id, err := tpgresource.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
}
if err := tpgresource.SetDataSourceLabels(d); err != nil {
return err
}
// pem_csr is only applicable for SUBORDINATE CertificateAuthorities when their state is AWAITING_USER_ACTIVATION
if d.Get("type") == "SUBORDINATE" && d.Get("state") == "AWAITING_USER_ACTIVATION" {
url, err := tpgresource.ReplaceVars(d, config, "{{PrivatecaBasePath}}projects/{{project}}/locations/{{location}}/caPools/{{pool}}/certificateAuthorities/{{certificate_authority_id}}:fetch")
if err != nil {
return err
}
billingProject := ""
project, err := tpgresource.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 := tpgresource.GetBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: userAgent,
})
if err != nil {
return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("PrivatecaCertificateAuthority %q", d.Id()), url)
}
if err := d.Set("pem_csr", res["pemCsr"]); err != nil {
return fmt.Errorf("Error fetching CertificateAuthority: %s", err)
}
}
return nil
}