generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
ssh_credential_data_source.go
68 lines (56 loc) · 1.88 KB
/
ssh_credential_data_source.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
package secrets
import (
"context"
"errors"
"github.com/harness/harness-go-sdk/harness/cd/graphql"
"github.com/harness/terraform-provider-harness/internal"
"github.com/harness/terraform-provider-harness/internal/service/cd/usagescope"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceSshCredential() *schema.Resource {
return &schema.Resource{
// This description is used by the documentation generator and the language server.
Description: "Data source for retrieving an SSH credential.",
ReadContext: dataSourceSshCredentialRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "Unique identifier of the secret manager",
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"name"},
AtLeastOneOf: []string{"name", "id"},
},
"name": {
Description: "The name of the secret manager",
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"id"},
AtLeastOneOf: []string{"name", "id"},
},
"usage_scope": usagescope.Schema(),
},
}
}
func dataSourceSshCredentialRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session)
var sshCred *graphql.SSHCredential
var err error
if id := d.Get("id").(string); id != "" {
sshCred, err = c.CDClient.SecretClient.GetSSHCredentialById(id)
} else if name := d.Get("name").(string); name != "" {
sshCred, err = c.CDClient.SecretClient.GetSSHCredentialByName(name)
} else if err != nil {
return diag.FromErr(err)
}
if err != nil {
return diag.FromErr(err)
}
if sshCred == nil {
return diag.FromErr(errors.New("could not find ssh credential"))
}
d.SetId(sshCred.Id)
d.Set("name", sshCred.Name)
d.Set("usage_scope", usagescope.FlattenUsageScope(sshCred.UsageScope))
return nil
}