Skip to content

Commit 8500d0e

Browse files
z0zaZoltan Illes
and
Zoltan Illes
authored
extend sdk with dedicated host operations (#356)
* add dedicated host related api calls with examples * add list dedicatedhostflavor api * rename hostpool id fields Co-authored-by: Zoltan Illes <[email protected]>
1 parent 701829d commit 8500d0e

File tree

20 files changed

+1592
-4
lines changed

20 files changed

+1592
-4
lines changed

api/container/containerv2/api_service.go

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type ContainerServiceAPI interface {
2828
Subnets() Subnets
2929
NlbDns() Nlbdns
3030
Satellite() Satellite
31+
DedicatedHost() DedicatedHost
32+
DedicatedHostPool() DedicatedHostPool
33+
DedicatedHostFlavor() DedicatedHostFlavor
3134

3235
//TODO Add other services
3336
}
@@ -129,3 +132,18 @@ func (c *csService) Workers() Workers {
129132
func (c *csService) Subnets() Subnets {
130133
return newSubnetsAPI(c.Client)
131134
}
135+
136+
//DedicatedHost implements DedicatedHost API
137+
func (c *csService) DedicatedHost() DedicatedHost {
138+
return newDedicatedHostAPI(c.Client)
139+
}
140+
141+
//DedicatedHostPool implements DedicatedHostPool API
142+
func (c *csService) DedicatedHostPool() DedicatedHostPool {
143+
return newDedicatedHostPoolAPI(c.Client)
144+
}
145+
146+
//DedicatedHostFlavor implements DedicatedHostFlavor API
147+
func (c *csService) DedicatedHostFlavor() DedicatedHostFlavor {
148+
return newDedicatedHostFlavorAPI(c.Client)
149+
}

api/container/containerv2/clusters.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type WorkerPoolConfig struct {
3636
DiskEncryption bool `json:"diskEncryption,omitempty"`
3737
Entitlement string `json:"entitlement"`
3838
Flavor string `json:"flavor"`
39+
HostPoolID string `json:"hostPoolID,omitempty"`
3940
Isolation string `json:"isolation,omitempty"`
4041
Labels map[string]string `json:"labels,omitempty"`
4142
Name string `json:"name" binding:"required" description:"The workerpool's name"`

api/container/containerv2/clusters_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ var _ = Describe("Clusters", func() {
205205
server.AppendHandlers(
206206
ghttp.CombineHandlers(
207207
ghttp.VerifyRequest(http.MethodPost, "/v2/vpc/createCluster"),
208-
ghttp.VerifyJSON(`{"disablePublicServiceEndpoint": false, "defaultWorkerPoolEntitlement": "", "kubeVersion": "", "podSubnet": "podnet", "provider": "abc", "serviceSubnet": "svcnet", "name": "abcd", "cosInstanceCRN": "", "workerPool": {"flavor": "", "name": "", "vpcID": "", "workerCount": 0, "zones": null, "entitlement": ""}}`),
208+
ghttp.VerifyJSON(`{"disablePublicServiceEndpoint": false, "defaultWorkerPoolEntitlement": "", "kubeVersion": "", "podSubnet": "podnet", "provider": "abc", "serviceSubnet": "svcnet", "name": "abcd", "cosInstanceCRN": "", "workerPool": {"flavor": "", "hostPoolID": "hostpoolid", "name": "", "vpcID": "", "workerCount": 0, "zones": null, "entitlement": ""}}`),
209209
ghttp.RespondWith(http.StatusCreated, `{
210210
"clusterID": "f91adfe2-76c9-4649-939e-b01c37a3704c"
211211
}`),
@@ -216,6 +216,7 @@ var _ = Describe("Clusters", func() {
216216
It("should return cluster created", func() {
217217
WPools := WorkerPoolConfig{
218218
Flavor: "", WorkerCount: 0, VpcID: "", Name: "",
219+
HostPoolID: "hostpoolid",
219220
}
220221
params := ClusterCreateRequest{
221222
DisablePublicServiceEndpoint: false, KubeVersion: "", PodSubnet: "podnet", Provider: "abc", ServiceSubnet: "svcnet", Name: "abcd", WorkerPools: WPools, CosInstanceCRN: "",
+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package containerv2
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/IBM-Cloud/bluemix-go/client"
7+
)
8+
9+
// CreateDedicatedHostRequest provides dedicated host data for create call
10+
// swagger:model
11+
type CreateDedicatedHostRequest struct {
12+
Flavor string `json:"flavor" description:""`
13+
HostPoolID string `json:"hostPoolID" description:""`
14+
Zone string `json:"zone" description:""`
15+
}
16+
17+
// CreateDedicatedHostResponse provides dedicated host id from create call
18+
// swagger:model
19+
type CreateDedicatedHostResponse struct {
20+
ID string `json:"id"`
21+
}
22+
23+
// GetDedicatedHostResponse provides dedicated host data from get call
24+
// swagger:model
25+
type GetDedicatedHostResponse struct {
26+
Flavor string `json:"flavor"`
27+
ID string `json:"id"`
28+
Lifecycle DedicatedHostLifecycle `json:"lifecycle"`
29+
PlacementEnabled bool `json:"placementEnabled"`
30+
Resources DedicatedHostResources `json:"resources"`
31+
Workers []DedicatedHostWorker `json:"workers"`
32+
Zone string `json:"zone"`
33+
}
34+
35+
// DedicatedHostLifecycle ...
36+
type DedicatedHostLifecycle struct {
37+
ActualState string `json:"actualState"`
38+
DesiredState string `json:"desiredState"`
39+
Message string `json:"message"`
40+
MessageDate string `json:"messageDate"`
41+
MessageDetails string `json:"messageDetails"`
42+
MessageDetailsDate string `json:"messageDetailsDate"`
43+
}
44+
45+
// DedicatedHostWorker ...
46+
type DedicatedHostWorker struct {
47+
ClusterID string `json:"clusterID"`
48+
Flavor string `json:"flavor"`
49+
WorkerID string `json:"workerID"`
50+
WorkerPoolID string `json:"workerPoolID"`
51+
}
52+
53+
// DedicatedHostResources ...
54+
type DedicatedHostResources struct {
55+
Capacity DedicatedHostResource `json:"capacity"`
56+
Consumed DedicatedHostResource `json:"consumed"`
57+
}
58+
59+
// DedicatedHostResource ...
60+
type DedicatedHostResource struct {
61+
MemoryBytes int64 `json:"memoryBytes"`
62+
VCPU int64 `json:"vcpu"`
63+
}
64+
65+
// RemoveDedicatedHostRequest provides dedicated host data for remove call
66+
// swagger:model
67+
type RemoveDedicatedHostRequest struct {
68+
HostID string `json:"host" description:""`
69+
HostPoolID string `json:"hostPool" description:""`
70+
}
71+
72+
// UpdateDedicatedHostPlacementRequest provides dedicated host data for update call
73+
// swagger:model
74+
type UpdateDedicatedHostPlacementRequest struct {
75+
HostPoolID string `json:"hostPoolID"`
76+
HostID string `json:"hostID"`
77+
}
78+
79+
//DedicatedHost ...
80+
type DedicatedHost interface {
81+
CreateDedicatedHost(dedicatedHostReq CreateDedicatedHostRequest, target ClusterTargetHeader) (CreateDedicatedHostResponse, error)
82+
GetDedicatedHost(dedicatedHostID, dedicatedHostPoolID string, target ClusterTargetHeader) (GetDedicatedHostResponse, error)
83+
ListDedicatedHosts(dedicatedHostPoolID string, target ClusterTargetHeader) ([]GetDedicatedHostResponse, error)
84+
RemoveDedicatedHost(dedicatedHostReq RemoveDedicatedHostRequest, target ClusterTargetHeader) error
85+
EnableDedicatedHostPlacement(dedicatedHostReq UpdateDedicatedHostPlacementRequest, target ClusterTargetHeader) error
86+
DisableDedicatedHostPlacement(dedicatedHostReq UpdateDedicatedHostPlacementRequest, target ClusterTargetHeader) error
87+
}
88+
89+
type dedicatedhost struct {
90+
client *client.Client
91+
}
92+
93+
func newDedicatedHostAPI(c *client.Client) DedicatedHost {
94+
return &dedicatedhost{
95+
client: c,
96+
}
97+
}
98+
99+
// GetDedicatedHost calls the API to list dedicated host s
100+
func (w *dedicatedhost) ListDedicatedHosts(dedicatedHostPoolID string, target ClusterTargetHeader) ([]GetDedicatedHostResponse, error) {
101+
successV := []GetDedicatedHostResponse{}
102+
_, err := w.client.Get(fmt.Sprintf("/v2/getDedicatedHosts?dedicatedhostpool=%s", dedicatedHostPoolID), &successV, target.ToMap())
103+
return successV, err
104+
}
105+
106+
// GetDedicatedHost calls the API to get a dedicated host
107+
func (w *dedicatedhost) GetDedicatedHost(dedicatedHostID, dedicatedHostPoolID string, target ClusterTargetHeader) (GetDedicatedHostResponse, error) {
108+
var successV GetDedicatedHostResponse
109+
_, err := w.client.Get(fmt.Sprintf("/v2/getDedicatedHost?dedicatedhost=%s&dedicatedhostpool=%s", dedicatedHostID, dedicatedHostPoolID), &successV, target.ToMap())
110+
return successV, err
111+
}
112+
113+
// CreateDedicatedHost calls the API to create a dedicated host
114+
func (w *dedicatedhost) CreateDedicatedHost(createDedicatedHostReq CreateDedicatedHostRequest, target ClusterTargetHeader) (CreateDedicatedHostResponse, error) {
115+
var successV CreateDedicatedHostResponse
116+
_, err := w.client.Post("/v2/createDedicatedHost", createDedicatedHostReq, &successV, target.ToMap())
117+
return successV, err
118+
}
119+
120+
// RemoveDedicatedHost calls the API to remove a dedicated host
121+
func (w *dedicatedhost) RemoveDedicatedHost(removeDedicatedHostReq RemoveDedicatedHostRequest, target ClusterTargetHeader) error {
122+
// Make the request, don't care about return value
123+
_, err := w.client.Post("/v2/removeDedicatedHost", removeDedicatedHostReq, nil, target.ToMap())
124+
return err
125+
}
126+
127+
// EnableDedicatedHostPlacement calls the API to enable placement on a dedicated host
128+
func (w *dedicatedhost) EnableDedicatedHostPlacement(updateDedicatedHostReq UpdateDedicatedHostPlacementRequest, target ClusterTargetHeader) error {
129+
// Make the request, don't care about return value
130+
_, err := w.client.Post("/v2/enableDedicatedHostPlacement", updateDedicatedHostReq, nil, target.ToMap())
131+
return err
132+
}
133+
134+
// DisableDedicatedHostPlacement calls the API to disable placement on a dedicated host
135+
func (w *dedicatedhost) DisableDedicatedHostPlacement(updateDedicatedHostReq UpdateDedicatedHostPlacementRequest, target ClusterTargetHeader) error {
136+
// Make the request, don't care about return value
137+
_, err := w.client.Post("/v2/disableDedicatedHostPlacement", updateDedicatedHostReq, nil, target.ToMap())
138+
return err
139+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package containerv2
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/IBM-Cloud/bluemix-go/client"
7+
)
8+
9+
// GetDedicatedHostFlavor is a response to a dedicated host pool get request.
10+
// swagger:model
11+
type GetDedicatedHostFlavor struct {
12+
ID string `json:"id"`
13+
FlavorClass string `json:"flavorClass"`
14+
Region string `json:"region"`
15+
Zone string `json:"zone"`
16+
Deprecated bool `json:"deprecated"`
17+
MaxVCPUs int `json:"maxVCPUs"`
18+
MaxMemory int `json:"maxMemory"`
19+
InstanceStorage []InstanceStorage `json:"instanceStorage"`
20+
}
21+
22+
// GetDedicatedHostFlavors is a response to a dedicated host pool list request.
23+
// swagger:model
24+
type GetDedicatedHostFlavors []GetDedicatedHostFlavor
25+
26+
// InstanceStorage type for describing an instance disk configuration
27+
// swagger:model
28+
type InstanceStorage struct {
29+
Count int `json:"count"`
30+
// the size of each individual device in GB
31+
Size int `json:"size"`
32+
}
33+
34+
//DedicatedHostFlavor ...
35+
type DedicatedHostFlavor interface {
36+
ListDedicatedHostFlavors(zone string, target ClusterTargetHeader) (GetDedicatedHostFlavors, error)
37+
}
38+
39+
type dedicatedhostflavor struct {
40+
client *client.Client
41+
}
42+
43+
func newDedicatedHostFlavorAPI(c *client.Client) DedicatedHostFlavor {
44+
return &dedicatedhostflavor{
45+
client: c,
46+
}
47+
}
48+
49+
// GetDedicatedHostFlavor calls the API to list dedicated host s
50+
func (w *dedicatedhostflavor) ListDedicatedHostFlavors(zone string, target ClusterTargetHeader) (GetDedicatedHostFlavors, error) {
51+
successV := GetDedicatedHostFlavors{}
52+
_, err := w.client.Get(fmt.Sprintf("/v2/getDedicatedHostFlavors?provider=vpc-gen2&zone=%s", zone), &successV, target.ToMap())
53+
return successV, err
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package containerv2
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
bluemix "github.com/IBM-Cloud/bluemix-go"
8+
"github.com/IBM-Cloud/bluemix-go/client"
9+
bluemixHttp "github.com/IBM-Cloud/bluemix-go/http"
10+
"github.com/IBM-Cloud/bluemix-go/session"
11+
"github.com/onsi/gomega/ghttp"
12+
13+
. "github.com/onsi/ginkgo"
14+
. "github.com/onsi/gomega"
15+
)
16+
17+
var _ = Describe("dedicatedhostflavor", func() {
18+
var server *ghttp.Server
19+
AfterEach(func() {
20+
server.Close()
21+
})
22+
23+
Describe("List", func() {
24+
Context("When list dedicatedhostflavors is successful", func() {
25+
BeforeEach(func() {
26+
server = ghttp.NewServer()
27+
server.AppendHandlers(
28+
ghttp.CombineHandlers(
29+
ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHostFlavors"),
30+
ghttp.RespondWith(http.StatusCreated, `[
31+
{
32+
"deprecated":false,
33+
"flavorClass":"flavorclass1",
34+
"id":"flavorid1",
35+
"instanceStorage":[
36+
{
37+
"count": 0,
38+
"size": 0
39+
}
40+
],
41+
"maxMemory": 52,
42+
"maxVCPUs": 12,
43+
"region": "region1",
44+
"zone": "zone1"
45+
}
46+
]`),
47+
),
48+
)
49+
})
50+
51+
It("should list dedicatedhostflavors in a zone", func() {
52+
target := ClusterTargetHeader{}
53+
54+
ldhf, err := newDedicatedHostFlavor(server.URL()).ListDedicatedHostFlavors("zone1", target)
55+
Expect(err).NotTo(HaveOccurred())
56+
expectedDedicatedHostFlavors := GetDedicatedHostFlavors{
57+
GetDedicatedHostFlavor{
58+
ID: "flavorid1",
59+
FlavorClass: "flavorclass1",
60+
Region: "region1",
61+
Zone: "zone1",
62+
Deprecated: false,
63+
MaxVCPUs: 12,
64+
MaxMemory: 52,
65+
InstanceStorage: []InstanceStorage{
66+
InstanceStorage{
67+
Count: 0,
68+
Size: 0,
69+
},
70+
},
71+
},
72+
}
73+
Expect(ldhf).To(BeEquivalentTo(expectedDedicatedHostFlavors))
74+
})
75+
})
76+
Context("When list dedicatedhostflavors is unsuccessful", func() {
77+
BeforeEach(func() {
78+
server = ghttp.NewServer()
79+
server.SetAllowUnhandledRequests(true)
80+
server.AppendHandlers(
81+
ghttp.CombineHandlers(
82+
ghttp.VerifyRequest(http.MethodGet, "/v2/getDedicatedHostFlavors"),
83+
ghttp.RespondWith(http.StatusInternalServerError, `Failed to list dedicatedhostflavors`),
84+
),
85+
)
86+
})
87+
88+
It("should return error during get dedicatedhosts", func() {
89+
target := ClusterTargetHeader{}
90+
_, err := newDedicatedHostFlavor(server.URL()).ListDedicatedHostFlavors("zone1", target)
91+
Expect(err).To(HaveOccurred())
92+
})
93+
})
94+
})
95+
96+
})
97+
98+
func newDedicatedHostFlavor(url string) DedicatedHostFlavor {
99+
100+
sess, err := session.New()
101+
if err != nil {
102+
log.Fatal(err)
103+
}
104+
conf := sess.Config.Copy()
105+
conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
106+
conf.Endpoint = &url
107+
108+
client := client.Client{
109+
Config: conf,
110+
ServiceName: bluemix.VpcContainerService,
111+
}
112+
return newDedicatedHostFlavorAPI(&client)
113+
}

0 commit comments

Comments
 (0)