forked from wdreeveii/terraform-provider-awx
/
data_source_credential.go
77 lines (67 loc) · 1.59 KB
/
data_source_credential.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
/*
Use this data source to query Credential by ID.
Example Usage
```hcl
*TBD*
```
*/
package awx
import (
"context"
"strconv"
awx "github.com/denouche/goawx/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceCredentialByID() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCredentialByIDRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
},
"tower_id": {
Type: schema.TypeInt,
Computed: true,
},
"username": {
Type: schema.TypeString,
Computed: true,
},
"kind": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceCredentialByIDRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
id := d.Get("id").(int)
cred, err := client.CredentialsService.GetCredentialsByID(id, map[string]string{})
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to fetch credential",
Detail: "The given credential ID is invalid or malformed",
})
}
d.Set("username", cred.Inputs["username"])
d.Set("kind", cred.Kind)
d.Set("tower_id", id)
d.Set("description", cred.Description)
d.Set("name", cred.Name)
d.SetId(strconv.Itoa(id))
// d.SetId(strconv.FormatInt(time.Now().Unix(), 10))
return diags
}