/
data_source_acl_policy.go
59 lines (51 loc) · 1.33 KB
/
data_source_acl_policy.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
package nomad
import (
"fmt"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"strings"
)
func dataSourceAclPolicy() *schema.Resource {
return &schema.Resource{
Read: dataSourceAclPolicyRead,
Schema: map[string]*schema.Schema{
"name": {
Description: "Name",
Type: schema.TypeString,
Required: true,
},
"description": {
Description: "Description",
Type: schema.TypeString,
Computed: true,
},
"rules": {
Description: "ACL Rules in HCL format",
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAclPolicyRead(d *schema.ResourceData, meta interface{}) error {
providerConfig := meta.(ProviderConfig)
client := providerConfig.client
name := d.Get("name").(string)
log.Printf("[DEBUG] Getting ACL Policy %q", name)
policy, _, err := client.ACLPolicies().Info(name, &api.QueryOptions{})
if err != nil {
// As of Nomad 0.4.1, the API client returns an error for 404
// rather than a nil result, so we must check this way.
if strings.Contains(err.Error(), "404") {
d.SetId("")
return nil
}
return fmt.Errorf("error getting ACL policy: %#v", err)
}
d.SetId(policy.Name)
d.Set("name", policy.Name)
d.Set("description", policy.Description)
d.Set("rules", policy.Rules)
return nil
}