/
data_source_edgecenter_secret.go
138 lines (124 loc) · 3.79 KB
/
data_source_edgecenter_secret.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
package edgecenter
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceSecret() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSecretRead,
Description: "Represent secret",
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeInt,
Optional: true,
Description: "The uuid of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"project_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"region_id": {
Type: schema.TypeInt,
Optional: true,
Description: "The uuid of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"region_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the secret.",
},
"algorithm": {
Type: schema.TypeString,
Computed: true,
Description: "The encryption algorithm used for the secret.",
},
"bit_length": {
Type: schema.TypeInt,
Computed: true,
Description: "The bit length of the encryption algorithm.",
},
"mode": {
Type: schema.TypeString,
Computed: true,
Description: "The mode of the encryption algorithm.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The current status of the secret.",
},
"content_types": {
Type: schema.TypeMap,
Computed: true,
Description: "The content types associated with the secret's payload.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"expiration": {
Type: schema.TypeString,
Description: "Datetime when the secret will expire. The format is 2025-12-28T19:14:44.180394",
Computed: true,
},
"created": {
Type: schema.TypeString,
Description: "Datetime when the secret was created. The format is 2025-12-28T19:14:44.180394",
Computed: true,
},
},
}
}
func dataSourceSecretRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start secret reading")
var diags diag.Diagnostics
config := m.(*Config)
clientV2 := config.CloudClient
regionID, projectID, err := GetRegionIDandProjectID(ctx, clientV2, d)
if err != nil {
return diag.FromErr(err)
}
clientV2.Region = regionID
clientV2.Project = projectID
secretID := d.Id()
log.Printf("[DEBUG] Secret id = %s", secretID)
allSecrets, _, err := clientV2.Secrets.List(ctx)
if err != nil {
return diag.Errorf("cannot get secrets. Error: %s", err.Error())
}
var found bool
name := d.Get("name").(string)
for _, secret := range allSecrets {
if name == secret.Name {
d.SetId(secret.ID)
d.Set("name", name)
d.Set("algorithm", secret.Algorithm)
d.Set("bit_length", secret.BitLength)
d.Set("mode", secret.Mode)
d.Set("status", secret.Status)
d.Set("expiration", secret.Expiration)
d.Set("created", secret.Created)
if err := d.Set("content_types", secret.ContentTypes); err != nil {
return diag.FromErr(err)
}
found = true
break
}
}
if !found {
return diag.Errorf("secret with name %s does not exit", name)
}
log.Println("[DEBUG] Finish secret reading")
return diags
}