-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
123 lines (99 loc) · 2.82 KB
/
config.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
package bigfix
import (
"crypto/tls"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/hashicorp/terraform/helper/schema"
)
// API methods
// GET :
var GET = "GET"
// POST :
var POST = "POST"
// PUT :
var PUT = "PUT"
// DELETE :
var DELETE = "DELETE"
// Config : BigFix login credentials structure
type Config struct {
ServerIP string
Port string
Username string
Password string
}
// BFXConfig : BFXConfig is per-provider, specifies where to connect to bigfix
func BFXConfig(d *schema.ResourceData) (interface{}, error) {
serverip := d.Get("server").(string)
// Check if field is not empty
if serverip == "" {
return nil, fmt.Errorf("Bigfix server ip not found ")
}
username := d.Get("username").(string)
// Check if field is not empty
if username == "" {
return nil, fmt.Errorf("Bigfix username not found")
}
password := d.Get("password").(string)
// Check if field is not empty
if password == "" {
return nil, fmt.Errorf("Bigfix password not found")
}
port := d.Get("port").(string)
// Check if field is not empty
if port == "" {
return nil, fmt.Errorf("Bigfix server port not found")
}
// Check connection to bfx server
bfxURL := ConnectBigFixAPI(serverip, port) //get url for connection
_, err := http.NewRequest(GET, bfxURL, nil)
if err != nil {
fmt.Println(err)
return nil, err
}
config := Config{
ServerIP: serverip,
Username: username,
Password: password,
Port: port,
}
return config, nil
}
// BfxConnection : Will Connect to BigFix Server
func (config Config) BfxConnection(method string, url string, buffer io.Reader) (*http.Response, error) {
//Initialize HTTPS client to skip SSL certificate verification
flagSSL := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
skipSslVerify := &http.Client{
Transport: flagSSL,
// set default timeout to 300 secs
Timeout: 300 * time.Second,
}
log.Println("[DEBUG] Initialize HTTPS client to skip SSL certificate verification.")
//make request
request, err := http.NewRequest(method, url, buffer)
if err != nil {
fmt.Println(err)
os.Exit(1)
return nil, err
}
log.Println("[DEBUG] make request to the BigFix.")
//Configure basic authentication and content-type
request.SetBasicAuth(config.Username, config.Password)
request.Header.Set("Content-Type", "application/xml")
// Make request to BigFix Rest API
response, err1 := skipSslVerify.Do(request)
if err1 != nil {
return nil, err1
}
//check the status of the request i.e. response
// Status = 200 //update 2XX to 3XX
if response.StatusCode >= http.StatusOK && response.StatusCode < 400 {
log.Println("[DEBUG] HTTP response OK ")
return response, nil
}
// catch all other status code
return nil, fmt.Errorf("\tMethod: %s \n \tURL: %s \n \tStatusCode: [%d] \n \tStatus: %s", method, url, response.StatusCode, response.Status)
}