-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatasource_computer.go
125 lines (112 loc) · 3.14 KB
/
datasource_computer.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package bigfix
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceComputer() *schema.Resource {
return &schema.Resource{
Read: dataSourceComputerRead,
Schema: map[string]*schema.Schema{
// required value
"name": {
Type: schema.TypeString,
Description: "Name of computer",
Required: true,
ForceNew: true,
},
// computed values
"id": {
Type: schema.TypeString,
Description: "ID of computer",
Computed: true,
Optional: true,
},
"ip_address": {
Type: schema.TypeString,
Description: "IP Address of computer",
Computed: true,
Optional: true,
},
"os": {
Type: schema.TypeString,
Description: "Operating System of computer",
Computed: true,
Optional: true,
},
"cpu": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"last_report_time": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"dns_name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"ram": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"subnet_address": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"computer_type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"relay": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
}
}
func dataSourceComputerRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(Config)
computerName := d.Get("name").(string)
// Get details of computer with computer name
log.Printf("[INFO] Reading Bigfix Computers")
url := GetComputerDetailAPI(config.ServerIP, config.Port, computerName)
Response, err := config.BfxConnection(GET, url, nil)
if err != nil {
return err
}
var computerdetailstruct ComputerDetails
if Response != nil {
defer Response.Body.Close()
data, err := ioutil.ReadAll(Response.Body)
if err != nil {
fmt.Println(err)
}
//log.Println("[DEBUG] Response is :")
//println(string(data))
xml.Unmarshal([]byte(data), &computerdetailstruct)
if computerdetailstruct.Query.Error != "" {
return fmt.Errorf("No computer found with name `%s`.\n\t Error of Relevance Query is : { %s } ", computerName, computerdetailstruct.Query.Error)
}
d.Set("id", computerdetailstruct.Query.Result.Tuple.Answer[1].Text)
d.Set("name", computerdetailstruct.Query.Result.Tuple.Answer[0].Text)
d.Set("ip_address", computerdetailstruct.Query.Result.Tuple.Answer[8].Text)
d.Set("os", computerdetailstruct.Query.Result.Tuple.Answer[2].Text)
d.Set("cpu", computerdetailstruct.Query.Result.Tuple.Answer[5].Text)
d.Set("last_report_time", computerdetailstruct.Query.Result.Tuple.Answer[7].Text)
d.Set("dns_name", computerdetailstruct.Query.Result.Tuple.Answer[4].Text)
d.Set("relay", computerdetailstruct.Query.Result.Tuple.Answer[6].Text)
d.Set("ram", computerdetailstruct.Query.Result.Tuple.Answer[3].Text)
d.SetId(computerdetailstruct.Query.Result.Tuple.Answer[1].Text)
}
return nil
}