-
Notifications
You must be signed in to change notification settings - Fork 7
/
httpclient_go.go
124 lines (105 loc) · 3.04 KB
/
httpclient_go.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
package httpclient_go
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/config"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
)
const (
MethodGet = "GET"
MethodPost = "POST"
MethodPut = "PUT"
MethodDelete = "DELETE"
)
type HttpClientGo struct {
Client *golangsdk.ServiceClient
Method string
Url string
RequestOpts golangsdk.RequestOpts
Header map[string]string
Error error
Transport *http.Transport
}
func NewHttpClientGo(c *config.HcsConfig, product, region string) (*HttpClientGo, error) {
client, err := c.NewServiceClient(product, region)
if err != nil {
return nil, err
}
return &HttpClientGo{
Client: client,
RequestOpts: golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{"content-type": "application/json"},
},
}, nil
}
func (client *HttpClientGo) WithMethod(method string) *HttpClientGo {
client.Method = method
return client
}
func (client *HttpClientGo) WithUrl(url string) *HttpClientGo {
client.Url = client.Client.Endpoint + url
return client
}
func (client *HttpClientGo) WithUrlWithoutEndpoint(cfg *config.HcsConfig, srv, region, path string) *HttpClientGo {
endpoint := config.GetServiceEndpoint(cfg, srv, region)
client.Url = endpoint + path
return client
}
func (client *HttpClientGo) WithBody(body interface{}) *HttpClientGo {
client.RequestOpts.JSONBody = body
return client
}
func (client *HttpClientGo) WithHeader(header map[string]string) *HttpClientGo {
if len(header) == 0 {
return client
}
client.RequestOpts.MoreHeaders = header
return client
}
func (client *HttpClientGo) WithOKCodes(arr []int) *HttpClientGo {
client.RequestOpts.OkCodes = arr
return client
}
func (client *HttpClientGo) Do() (*http.Response, error) {
return client.Client.Request(client.Method, client.Url, &client.RequestOpts)
}
func (client *HttpClientGo) WithTransport() *HttpClientGo {
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS12,
},
}
return client
}
func (client HttpClientGo) CheckDeletedDiag(d *schema.ResourceData, err error, response *http.Response, msg string) ([]byte, diag.Diagnostics) {
if err != nil {
return nil, diag.Errorf("%s: %s", msg, err)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, diag.Errorf("error convert data %s: %s", string(body), err)
}
if response.StatusCode == 200 {
return body, nil
}
if strings.Contains(string(body), "does not exist") {
resourceID := d.Id()
d.SetId("")
return nil, diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "Resource not found",
Detail: fmt.Sprintf("the resource %s is goneand will be removed in Terraform state.", resourceID),
},
}
}
return nil, diag.Errorf("%s: %s", msg, err)
}