forked from denouche/terraform-provider-awx
/
data_source_credential_type.go
77 lines (67 loc) · 1.63 KB
/
data_source_credential_type.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 Type by ID.
Example Usage
```hcl
*TBD*
```
*/
package awx
import (
"context"
"fmt"
"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 dataSourceCredentialTypeByID() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCredentialTypeByIDRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"kind": {
Type: schema.TypeString,
Computed: true,
},
"inputs": {
Type: schema.TypeString,
Computed: true,
},
"injectors": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceCredentialTypeByIDRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
id := d.Get("id").(int)
credType, err := client.CredentialTypeService.GetCredentialTypeByID(id, map[string]string{})
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to fetch credential type",
Detail: fmt.Sprintf("Unable to fetch credential type with ID: %d. Error: %s", id, err.Error()),
})
}
d.Set("name", credType.Name)
d.Set("description", credType.Description)
d.Set("kind", credType.Kind)
d.Set("inputs", credType.Inputs)
d.Set("injectors", credType.Injectors)
d.SetId(strconv.Itoa(id))
return diags
}