-
Notifications
You must be signed in to change notification settings - Fork 46
/
action.go
145 lines (129 loc) · 4.42 KB
/
action.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package rdspostgresql
import (
"encoding/json"
"fmt"
"github.com/volcengine/volc-sdk-golang/base"
)
const (
ActionCreateInstance = "CreateDBInstance"
ActionCreateIpWhiteList = "CreateDBInstanceIPList"
ActionCreateAccount = "CreateAccount"
ActionCreateDatabase = "CreateDatabase"
ActionModifyDatabaseOwner = "ModifyDatabaseOwner"
)
const (
APIVersion20180101 = "2018-01-01"
)
// Create instance
func (p *RdsPostgresql) CreateInstance(req *CreateInstanceReq) (*CreateInstanceResp, error) {
setCreateDefaultValues(req, false)
reqData, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("CreateInstance: fail to marshal request, %v", err)
}
respBody, _, err := p.Client.Json(ActionCreateInstance, nil, string(reqData))
if err != nil {
return nil, fmt.Errorf("CreateDBInstance: fail to do request, %v", err)
}
result := new(CreateInstanceResp)
if err := UnmarshalResultInto(respBody, result); err != nil {
return nil, err
}
return result, nil
}
// Create instance
func (p *RdsPostgresql) CreateROInstance(req *CreateInstanceReq) (*CreateInstanceResp, error) {
setCreateDefaultValues(req, true)
reqData, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("CreateInstance: fail to marshal request, %v", err)
}
respBody, _, err := p.Client.Json(ActionCreateInstance, nil, string(reqData))
if err != nil {
return nil, fmt.Errorf("CreateDBInstance: fail to do request, %v", err)
}
result := new(CreateInstanceResp)
if err := UnmarshalResultInto(respBody, result); err != nil {
return nil, err
}
return result, nil
}
func setCreateDefaultValues(req *CreateInstanceReq, isReadonly bool) {
if isReadonly {
req.InstanceCategory = "ReadOnly"
req.InstanceType = "Basic"
} else {
req.InstanceCategory = "Primary"
req.InstanceType = "HA"
}
}
// Create instance IP white list
func (p *RdsPostgresql) CreateDBInstanceIPList(req *CreateDBInstanceIPListReq) (*base.CommonResponse, error) {
reqData, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("CreateDBInstanceIPList: fail to marshal request, %v", err)
}
respBody, _, err := p.Client.Json(ActionCreateIpWhiteList, nil, string(reqData))
if err != nil {
return nil, fmt.Errorf("CreateDBInstanceIPList: fail to do request, %v", err)
}
resp := new(base.CommonResponse)
if err := json.Unmarshal(respBody, &resp); err != nil {
return resp, fmt.Errorf("CreateDBInstanceIPList fail to unmarshal response, %v", err)
}
return resp, nil
}
// Create account
func (p *RdsPostgresql) CreateAccount(req *CreateAccountReq) (*base.CommonResponse, error) {
fmt.Println(req)
reqData, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("CreateAccount: fail to marshal request, %v", err)
}
fmt.Println(string(reqData))
respBody, _, err := p.Client.Json(ActionCreateAccount, nil, string(reqData))
if err != nil {
return nil, fmt.Errorf("CreateAccount: fail to do request, %v", err)
}
fmt.Println(string(respBody))
resp := new(base.CommonResponse)
if err := json.Unmarshal(respBody, &resp); err != nil {
return resp, fmt.Errorf("CreateAccount fail to unmarshal response, %v", err)
}
return resp, nil
}
// Create account
func (p *RdsPostgresql) CreateDatabase(req *CreateDatabaseReq) (*base.CommonResponse, error) {
fmt.Println(req)
reqData, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("CreateDatabase: fail to marshal request, %v", err)
}
respBody, _, err := p.Client.Json(ActionCreateDatabase, nil, string(reqData))
if err != nil {
fmt.Println(string(respBody))
return nil, fmt.Errorf("CreateDatabase: fail to do request, %v", err)
}
fmt.Println(string(respBody))
resp := new(base.CommonResponse)
if err := json.Unmarshal(respBody, &resp); err != nil {
return resp, fmt.Errorf("CreateDatabase fail to unmarshal response, %v", err)
}
return resp, nil
}
// Modify database owner
func (p *RdsPostgresql) ModifyDatabaseOwner(req *ModifyDatabaseOwnerReq) (*base.CommonResponse, error) {
reqData, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("ModifyDatabaseOwner: fail to marshal request, %v", err)
}
respBody, _, err := p.Client.Json(ActionModifyDatabaseOwner, nil, string(reqData))
if err != nil {
return nil, fmt.Errorf("ModifyDatabaseOwner: fail to do request, %v", err)
}
resp := new(base.CommonResponse)
if err := json.Unmarshal(respBody, &resp); err != nil {
return resp, fmt.Errorf("ModifyDatabaseOwner fail to unmarshal response, %v", err)
}
return resp, nil
}