-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatasource_fixlet.go
79 lines (68 loc) · 1.86 KB
/
datasource_fixlet.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
package bigfix
import (
"encoding/xml"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"io/ioutil"
"log"
)
func dataSourceFixlet() *schema.Resource {
return &schema.Resource{
Read: dataSourceFixletRead,
Schema: map[string]*schema.Schema{
//required value
"name": {
Type: schema.TypeString,
Description: "title of fixlet",
Required: true,
ForceNew: true,
},
// optional value
"id": {
Type: schema.TypeString,
Optional: true,
},
"site_name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"default_action": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
}
}
func dataSourceFixletRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(Config)
name := d.Get("name").(string)
// Check whether Fixlet exists with provided name
log.Println("[DEBUG] Reading List of Fixlets...")
url := GetFixletMetaDataAPI(config.ServerIP, config.Port, name)
response, err := config.BfxConnection(GET, url, nil)
if err != nil {
return err
}
var fixletMetaDataStruct FixletMetaData
if response != nil {
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
log.Println("[DEBUG] response is :")
log.Println(string(data))
xml.Unmarshal([]byte(data), &fixletMetaDataStruct)
if fixletMetaDataStruct.Query.Result.Tuple.Text == "" {
return fmt.Errorf("No Fixlet found with name `%s`", name)
}
d.Set("name", fixletMetaDataStruct.Query.Result.Tuple.Answer[2].Text)
d.Set("id", fixletMetaDataStruct.Query.Result.Tuple.Answer[1].Text)
d.Set("site_name", fixletMetaDataStruct.Query.Result.Tuple.Answer[0].Text)
d.Set("default_action", fixletMetaDataStruct.Query.Result.Tuple.Answer[3].Text)
d.SetId(fixletMetaDataStruct.Query.Result.Tuple.Answer[1].Text)
}
return nil
}