File: machine_group.go

package info (click to toggle)
golang-github-denverdino-aliyungo 0.0~git20180921.13fa8aa-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,824 kB
  • sloc: xml: 1,359; makefile: 3
file content (158 lines) | stat: -rw-r--r-- 3,993 bytes parent folder | download | duplicates (3)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
package sls

import (
	"encoding/json"
	"errors"
	"fmt"
	"strconv"
)

type GroupAttribute struct {
	Topic        string `json:"groupTopic,omitempty"`
	ExternalName string `json:"externalName,omitempty"`
}

type MachineGroup struct {
	Name                string          `json:"groupName,omitempty"`
	Type                string          `json:"groupType,omitempty"`
	MachineIdentifyType string          `json:"machineIdentifyType,omitempty"`
	Attribute           *GroupAttribute `json:"attribute,omitempty"`
	MachineList         []string        `json:"machineList,omitempty"`
	CreateTime          uint32          `json:"createTime,omitempty"`
	LastModifyTime      uint32          `json:"lastModifyTime,omitempty"`
}

func (proj *Project) CreateMachineGroup(machineGroup *MachineGroup) error {

	data, err := json.Marshal(machineGroup)
	if err != nil {
		return err
	}
	req := &request{
		method:      METHOD_POST,
		path:        "/machinegroups",
		payload:     data,
		contentType: "application/json",
	}

	return proj.client.requestWithClose(req)
}

type MachineGroupList struct {
	Groups []string `json:"machinegroups"`
	Count  int      `json:"count"`
	Total  int      `json:"total"`
}

func (proj *Project) ListMachineGroup(offset, size int) (*MachineGroupList, error) {
	req := &request{
		path:   "/machinegroups",
		method: METHOD_GET,
		params: map[string]string{
			"size":   strconv.Itoa(size),
			"offset": strconv.Itoa(offset),
		},
	}

	groups := &MachineGroupList{}

	if err := proj.client.requestWithJsonResponse(req, groups); err != nil {
		return nil, err
	}

	return groups, nil
}

func (proj *Project) MachineGroup(name string) (*MachineGroup, error) {
	req := &request{
		method: METHOD_GET,
		path:   "/machinegroups/" + name,
	}

	group := &MachineGroup{}

	if err := proj.client.requestWithJsonResponse(req, group); err != nil {
		return nil, err
	}

	return group, nil
}

func (proj *Project) DeleteMachineGroup(name string) error {
	req := &request{
		method: METHOD_DELETE,
		path:   "/machinegroups/" + name,
	}

	return proj.client.requestWithClose(req)
}

func (proj *Project) UpdateMachineGroup(machineGroup *MachineGroup) error {
	data, err := json.Marshal(machineGroup)
	if err != nil {
		return err
	}
	req := &request{
		method:      METHOD_PUT,
		path:        "/machinegroups/" + machineGroup.Name,
		payload:     data,
		contentType: "application/json",
	}

	return proj.client.requestWithClose(req)
}

func (proj *Project) ApplyConfigToMachineGroup(machineGroup string, config string) error {
	req := &request{
		method: METHOD_PUT,
		path:   "/machinegroups/" + machineGroup + "/configs/" + config,
	}
	return proj.client.requestWithClose(req)
}

type Machine struct {
	Ip            string `json:"ip,omitempty"`
	Uniqueid      string `json:"machine-uniqueid,omitempty"`
	UserdefinedId string `json:"userdefined-id,omitempty"`
}

type MachineList struct {
	Count    int       `json:"count,omitempty"`
	Total    int       `json:"total,omitempty"`
	Machines []Machine `json:"machines,omitempty"`
}

func (proj *Project) ListMachines(machineGroup string, offset, size int) (*MachineList, error) {
	req := &request{
		method: METHOD_GET,
		path:   "/machinegroups/" + machineGroup + "/machines",
		params: map[string]string{
			"size":   strconv.Itoa(size),
			"offset": strconv.Itoa(offset),
		},
	}
	//list := &MachineList{ machines:[]Machine{} }
	list := &MachineList{}
	if err := proj.client.requestWithJsonResponse(req, list); err != nil {
		return nil, err
	}
	return list, nil
}

func (proj *Project) GetAppliedConfigs(machineGroup string) ([]string, error) {
	req := &request{
		method: METHOD_GET,
		path:   "/machinegroups/" + machineGroup + "/configs",
	}

	configs := make(map[string]interface{})
	if err := proj.client.requestWithJsonResponse(req, configs); err != nil {
		return nil, err
	}

	if v, ok := configs["config"].([]string); ok {
		return v, nil
	}

	return nil, errors.New(fmt.Sprintf("%v is not a string array", configs["config"]))
}