generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
data_source_apiKey.go
84 lines (71 loc) · 2.25 KB
/
data_source_apiKey.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
package apikey
import (
"context"
"errors"
"net/http"
"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"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func DataSourceApiKey() *schema.Resource {
resource := &schema.Resource{
Description: "Data source for retrieving a Harness ApiKey.",
ReadContext: dataSourceApiKeyRead,
Schema: map[string]*schema.Schema{
"apikey_type": {
Description: "Type of the API Key",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"USER", "SERVICE_ACCOUNT"}, false),
},
"parent_id": {
Description: "Parent Identifier for the Entity",
Type: schema.TypeString,
Required: true,
},
"default_time_to_expire_token": {
Description: "Expiry time of the apiKey",
Type: schema.TypeInt,
Optional: true,
},
"account_id": {
Description: "Account Identifier for the Entity",
Type: schema.TypeString,
Required: true,
},
},
}
helpers.SetMultiLevelDatasourceSchemaIdentifierRequired(resource.Schema)
return resource
}
func dataSourceApiKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var apiKey *nextgen.ApiKey
id := d.Get("identifier").(string)
type_ := d.Get("apikey_type").(string)
parentId := d.Get("parent_id").(string)
if id != "" {
var err error
var httpResp *http.Response
resp, httpResp, err := c.ApiKeyApi.GetAggregatedApiKey(ctx, c.AccountId, type_, parentId, id, &nextgen.ApiKeyApiGetAggregatedApiKeyOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
apiKey = resp.Data.ApiKey
if apiKey == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
} else {
return diag.FromErr(errors.New("Identifier must be specified"))
}
readApiKey(d, apiKey)
return nil
}