/
data_source_ibm_function_rule.go
96 lines (84 loc) · 2.24 KB
/
data_source_ibm_function_rule.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
85
86
87
88
89
90
91
92
93
94
95
96
package ibm
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceIBMFunctionRule() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMFunctionRuleRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the rule.",
},
"namespace": {
Type: schema.TypeString,
Required: true,
Description: "Name of the namespace.",
},
"trigger_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the trigger.",
},
"action_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of an action.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "Status of the rule.",
},
"publish": {
Type: schema.TypeBool,
Computed: true,
Description: "Rule Visibility.",
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: "Semantic version of the rule",
},
"rule_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceIBMFunctionRuleRead(d *schema.ResourceData, meta interface{}) error {
wskClient, err := meta.(ClientSession).FunctionClient()
if err != nil {
return err
}
bxSession, err := meta.(ClientSession).BluemixSession()
if err != nil {
return err
}
namespace := d.Get("namespace").(string)
wskClient, err = setupOpenWhiskClientConfig(namespace, bxSession.Config, wskClient)
if err != nil {
return err
}
ruleService := wskClient.Rules
name := d.Get("name").(string)
rule, _, err := ruleService.Get(name)
if err != nil {
return fmt.Errorf("Error retrieving IBM Cloud Function Rule %s : %s", name, err)
}
d.SetId(rule.Name)
d.Set("name", rule.Name)
d.Set("namespace", namespace)
d.Set("publish", rule.Publish)
d.Set("version", rule.Version)
d.Set("status", rule.Status)
d.Set("rule_id", rule.Name)
d.Set("trigger_name", rule.Trigger.(map[string]interface{})["name"])
path := rule.Action.(map[string]interface{})["path"]
actionName := rule.Action.(map[string]interface{})["name"]
d.Set("action_name", fmt.Sprintf("/%s/%s", path, actionName))
return nil
}