generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 42
/
data_source_gnupg.go
139 lines (129 loc) · 4.27 KB
/
data_source_gnupg.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package gnupg
import (
"context"
"github.com/antihax/optional"
hh "github.com/harness/harness-go-sdk/harness/helpers"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceGitopsGnupg() *schema.Resource {
resource := &schema.Resource{
Description: "Data source for retrieving a GPG public key in the server's configuration.",
ReadContext: dataSourceGitopsGnupgRead,
Schema: map[string]*schema.Schema{
"account_id": {
Description: "Account Identifier for the GnuPG Key.",
Type: schema.TypeString,
Required: true,
},
"org_id": {
Description: "Organization Identifier for the GnuPG Key.",
Type: schema.TypeString,
Optional: true,
},
"project_id": {
Description: "Project Identifier for the GnuPG Key.",
Type: schema.TypeString,
Optional: true,
},
"agent_id": {
Description: "Agent identifier for the GnuPG Key.",
Type: schema.TypeString,
Required: true,
},
"identifier": {
Description: "Identifier for the GnuPG Key.",
Type: schema.TypeString,
Optional: true,
},
"request": {
Description: "GnuPGPublicKey is a representation of a GnuPG public key",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"upsert": {
Description: "Indicates if the GnuPG Key should be inserted if not present or updated if present.",
Type: schema.TypeBool,
Optional: true,
},
"publickey": {
Description: "Public key details.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key_id": {
Description: "KeyID specifies the key ID, in hexadecimal string format.",
Type: schema.TypeString,
Optional: true,
},
"fingerprint": {
Description: "Fingerprint is the fingerprint of the key",
Type: schema.TypeString,
Optional: true,
},
"owner": {
Description: "Owner holds the owner identification, e.g. a name and e-mail address",
Type: schema.TypeString,
Optional: true,
},
"trust": {
Description: "Trust holds the level of trust assigned to this key",
Type: schema.TypeString,
Optional: true,
},
"sub_type": {
Description: "SubType holds the key's sub type",
Type: schema.TypeString,
Optional: true,
},
"key_data": {
Description: "KeyData holds the raw key data, in base64 encoded format",
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
},
},
}
return resource
}
func dataSourceGitopsGnupgRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
ctx = context.WithValue(ctx, nextgen.ContextAccessToken, hh.EnvVars.BearerToken.Get())
var agentIdentifier, orgIdentifier, projectIdentifier string
keyId := d.Get("identifier").(string)
if attr, ok := d.GetOk("agent_id"); ok {
agentIdentifier = attr.(string)
}
if attr, ok := d.GetOk("project_id"); ok {
projectIdentifier = attr.(string)
}
if attr, ok := d.GetOk("org_id"); ok {
orgIdentifier = attr.(string)
}
resp, httpResp, err := c.GnuPGPKeysApi.AgentGPGKeyServiceGet(ctx, agentIdentifier, keyId, c.AccountId, &nextgen.GnuPGPKeysApiAgentGPGKeyServiceGetOpts{
OrgIdentifier: optional.NewString(orgIdentifier),
ProjectIdentifier: optional.NewString(projectIdentifier),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
// Soft delete lookup error handling
// https://harness.atlassian.net/browse/PL-23765
if &resp == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
readGnupgKey(d, &resp, c.AccountId, agentIdentifier, orgIdentifier, projectIdentifier)
return nil
}