/
resource_aviatrix_proxy_config.go
98 lines (82 loc) · 2.61 KB
/
resource_aviatrix_proxy_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
package aviatrix
import (
"fmt"
"strings"
"github.com/AviatrixSystems/terraform-provider-aviatrix/v2/goaviatrix"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAviatrixProxyConfig() *schema.Resource {
return &schema.Resource{
Create: resourceAviatrixProxyConfigCreate,
Read: resourceAviatrixProxyConfigRead,
Delete: resourceAviatrixProxyConfigDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"http_proxy": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "http proxy URL.",
},
"https_proxy": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "https proxy URL.",
},
"proxy_ca_certificate": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Server CA Certificate file.",
},
},
}
}
func resourceAviatrixProxyConfigCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
proxy := marshalProxyConfigInput(d)
if err := client.CreateProxyConfig(proxy); err != nil {
return fmt.Errorf("could not config proxy: %v", err)
}
d.SetId(strings.Replace(client.ControllerIP, ".", "-", -1))
return resourceAviatrixProxyConfigRead(d, meta)
}
func resourceAviatrixProxyConfigRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
if d.Id() != strings.Replace(client.ControllerIP, ".", "-", -1) {
return fmt.Errorf("ID: %s does not match controller IP. Please provide correct ID for importing", d.Id())
}
proxy, err := client.GetProxyConfig()
if err != nil {
if err == goaviatrix.ErrNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("couldn't get proxy configuration: %s", err)
}
d.Set("http_proxy", proxy.HttpProxy)
d.Set("https_proxy", proxy.HttpsProxy)
if proxy.ProxyCaCertificate != "" {
d.Set("proxy_ca_certificate", proxy.ProxyCaCertificate)
}
d.SetId(strings.Replace(client.ControllerIP, ".", "-", -1))
return nil
}
func resourceAviatrixProxyConfigDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
err := client.DeleteProxyConfig()
if err != nil {
return fmt.Errorf("failed to delete proxy configration: %s", err)
}
return nil
}
func marshalProxyConfigInput(d *schema.ResourceData) *goaviatrix.ProxyConfig {
return &goaviatrix.ProxyConfig{
HttpProxy: d.Get("http_proxy").(string),
HttpsProxy: d.Get("https_proxy").(string),
ProxyCaCertificate: d.Get("proxy_ca_certificate").(string),
}
}