| 1 |
package sls |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"strconv" |
| 8 |
) |
| 9 |
|
| 10 |
type GroupAttribute struct { |
| 11 |
Topic string `json:"groupTopic,omitempty"` |
| 12 |
ExternalName string `json:"externalName,omitempty"` |
| 13 |
} |
| 14 |
|
| 15 |
type MachineGroup struct { |
| 16 |
Name string `json:"groupName,omitempty"` |
| 17 |
Type string `json:"groupType,omitempty"` |
| 18 |
MachineIdentifyType string `json:"machineIdentifyType,omitempty"` |
| 19 |
Attribute *GroupAttribute `json:"attribute,omitempty"` |
| 20 |
MachineList []string `json:"machineList,omitempty"` |
| 21 |
CreateTime uint32 `json:"createTime,omitempty"` |
| 22 |
LastModifyTime uint32 `json:"lastModifyTime,omitempty"` |
| 23 |
} |
| 24 |
|
| 25 |
func (proj *Project) CreateMachineGroup(machineGroup *MachineGroup) error { |
| 26 |
|
| 27 |
data, err := json.Marshal(machineGroup) |
| 28 |
if err != nil { |
| 29 |
return err |
| 30 |
} |
| 31 |
req := &request{ |
| 32 |
method: METHOD_POST, |
| 33 |
path: "/machinegroups", |
| 34 |
payload: data, |
| 35 |
contentType: "application/json", |
| 36 |
} |
| 37 |
|
| 38 |
return proj.client.requestWithClose(req) |
| 39 |
} |
| 40 |
|
| 41 |
type MachineGroupList struct { |
| 42 |
Groups []string `json:"machinegroups"` |
| 43 |
Count int `json:"count"` |
| 44 |
Total int `json:"total"` |
| 45 |
} |
| 46 |
|
| 47 |
func (proj *Project) ListMachineGroup(offset, size int) (*MachineGroupList, error) { |
| 48 |
req := &request{ |
| 49 |
path: "/machinegroups", |
| 50 |
method: METHOD_GET, |
| 51 |
params: map[string]string{ |
| 52 |
"size": strconv.Itoa(size), |
| 53 |
"offset": strconv.Itoa(offset), |
| 54 |
}, |
| 55 |
} |
| 56 |
|
| 57 |
groups := &MachineGroupList{} |
| 58 |
|
| 59 |
if err := proj.client.requestWithJsonResponse(req, groups); err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
|
| 63 |
return groups, nil |
| 64 |
} |
| 65 |
|
| 66 |
func (proj *Project) MachineGroup(name string) (*MachineGroup, error) { |
| 67 |
req := &request{ |
| 68 |
method: METHOD_GET, |
| 69 |
path: "/machinegroups/" + name, |
| 70 |
} |
| 71 |
|
| 72 |
group := &MachineGroup{} |
| 73 |
|
| 74 |
if err := proj.client.requestWithJsonResponse(req, group); err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
|
| 78 |
return group, nil |
| 79 |
} |
| 80 |
|
| 81 |
func (proj *Project) DeleteMachineGroup(name string) error { |
| 82 |
req := &request{ |
| 83 |
method: METHOD_DELETE, |
| 84 |
path: "/machinegroups/" + name, |
| 85 |
} |
| 86 |
|
| 87 |
return proj.client.requestWithClose(req) |
| 88 |
} |
| 89 |
|
| 90 |
func (proj *Project) UpdateMachineGroup(machineGroup *MachineGroup) error { |
| 91 |
data, err := json.Marshal(machineGroup) |
| 92 |
if err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
req := &request{ |
| 96 |
method: METHOD_PUT, |
| 97 |
path: "/machinegroups/" + machineGroup.Name, |
| 98 |
payload: data, |
| 99 |
contentType: "application/json", |
| 100 |
} |
| 101 |
|
| 102 |
return proj.client.requestWithClose(req) |
| 103 |
} |
| 104 |
|
| 105 |
func (proj *Project) ApplyConfigToMachineGroup(machineGroup string, config string) error { |
| 106 |
req := &request{ |
| 107 |
method: METHOD_PUT, |
| 108 |
path: "/machinegroups/" + machineGroup + "/configs/" + config, |
| 109 |
} |
| 110 |
return proj.client.requestWithClose(req) |
| 111 |
} |
| 112 |
|
| 113 |
type Machine struct { |
| 114 |
Ip string `json:"ip,omitempty"` |
| 115 |
Uniqueid string `json:"machine-uniqueid,omitempty"` |
| 116 |
UserdefinedId string `json:"userdefined-id,omitempty"` |
| 117 |
} |
| 118 |
|
| 119 |
type MachineList struct { |
| 120 |
Count int `json:"count,omitempty"` |
| 121 |
Total int `json:"total,omitempty"` |
| 122 |
Machines []Machine `json:"machines,omitempty"` |
| 123 |
} |
| 124 |
|
| 125 |
func (proj *Project) ListMachines(machineGroup string, offset, size int) (*MachineList, error) { |
| 126 |
req := &request{ |
| 127 |
method: METHOD_GET, |
| 128 |
path: "/machinegroups/" + machineGroup + "/machines", |
| 129 |
params: map[string]string{ |
| 130 |
"size": strconv.Itoa(size), |
| 131 |
"offset": strconv.Itoa(offset), |
| 132 |
}, |
| 133 |
} |
| 134 |
|
| 135 |
list := &MachineList{} |
| 136 |
if err := proj.client.requestWithJsonResponse(req, list); err != nil { |
| 137 |
return nil, err |
| 138 |
} |
| 139 |
return list, nil |
| 140 |
} |
| 141 |
|
| 142 |
func (proj *Project) GetAppliedConfigs(machineGroup string) ([]string, error) { |
| 143 |
req := &request{ |
| 144 |
method: METHOD_GET, |
| 145 |
path: "/machinegroups/" + machineGroup + "/configs", |
| 146 |
} |
| 147 |
|
| 148 |
configs := make(map[string]interface{}) |
| 149 |
if err := proj.client.requestWithJsonResponse(req, configs); err != nil { |
| 150 |
return nil, err |
| 151 |
} |
| 152 |
|
| 153 |
if v, ok := configs["config"].([]string); ok { |
| 154 |
return v, nil |
| 155 |
} |
| 156 |
|
| 157 |
return nil, errors.New(fmt.Sprintf("%v is not a string array", configs["config"])) |
| 158 |
} |