Skip to content
This repository was archived by the owner on Nov 9, 2020. It is now read-only.

Commit a76b175

Browse files
chiragk25Gerrit Code Review
authored andcommitted
Moved deployment commands to system and infrastructure
- Removed all deployment commands - Commands added to system command - - deployment list-vms <id> -> system list-vms - deployment enable-service-type <id> -> system enable-service-type - deployment disable-service-type <id> -> system disable-service-type - deployment configure-nsx <id> -> system configure-nsx - Added new command infrastructure - deployment list-hosts <id> -> infrastructure list-hosts - deployment update-image-datastores -> infrastructure update-image-datastores - deployment sync-hosts-config -> infrastructure sync-hosts-config - Removed: deployment list Change-Id: I24f2fa814ace97914c63d027eea647cefb5254fc
1 parent f6c5176 commit a76b175

File tree

9 files changed

+1167
-1251
lines changed

9 files changed

+1167
-1251
lines changed

photon/command/deployments.go

Lines changed: 0 additions & 756 deletions
This file was deleted.

photon/command/deployments_test.go

Lines changed: 0 additions & 466 deletions
This file was deleted.

photon/command/infrastructure.go

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Copyright (c) 2016 VMware, Inc. All Rights Reserved.
2+
//
3+
// This product is licensed to you under the Apache License, Version 2.0 (the "License").
4+
// You may not use this product except in compliance with the License.
5+
//
6+
// This product may include a number of subcomponents with separate copyright notices and
7+
// license terms. Your use of these subcomponents is subject to the terms and conditions
8+
// of the subcomponent's license, as noted in the LICENSE file.
9+
10+
package command
11+
12+
import (
13+
"fmt"
14+
"io"
15+
"log"
16+
"os"
17+
"regexp"
18+
19+
"github.com/vmware/photon-controller-cli/photon/client"
20+
"github.com/vmware/photon-controller-cli/photon/utils"
21+
22+
"github.com/urfave/cli"
23+
"github.com/vmware/photon-controller-go-sdk/photon"
24+
)
25+
26+
// Creates a cli.Command for infrastructure
27+
// Subcommands:
28+
// list-hosts; Usage: infrastructure list-hosts [<id>]
29+
// update-image-datastores; Usage: infrastructure update-image-datastores [<options>]
30+
// sync-hosts-config; Usage: infrastructure sync-hosts-config
31+
32+
func GetInfrastructureCommand() cli.Command {
33+
command := cli.Command{
34+
Name: "infrastructure",
35+
Usage: "options for infrastructure",
36+
Subcommands: []cli.Command{
37+
{
38+
Name: "list-hosts",
39+
Usage: "Lists all ESXi hosts",
40+
ArgsUsage: " ",
41+
Description: "List information about all ESXi hosts used in the infrastructure.\n" +
42+
" For each host, the ID, the current state, the IP, and the type (MGMT and/or CLOUD)\n" +
43+
" Requires system administrator access.",
44+
Action: func(c *cli.Context) {
45+
err := listInfrastructureHosts(c, os.Stdout)
46+
if err != nil {
47+
log.Fatal("Error: ", err)
48+
}
49+
},
50+
},
51+
{
52+
Name: "update-image-datastores",
53+
Usage: "Updates the list of image datastores",
54+
ArgsUsage: " ",
55+
Description: "Update the list of allowed image datastores.\n" +
56+
" Requires system administrator access.",
57+
Flags: []cli.Flag{
58+
cli.StringFlag{
59+
Name: "datastores, d",
60+
Usage: "Comma separated name of datastore names",
61+
},
62+
},
63+
Action: func(c *cli.Context) {
64+
err := updateInfrastructureImageDatastores(c)
65+
if err != nil {
66+
log.Fatal("Error: ", err)
67+
}
68+
},
69+
},
70+
{
71+
Name: "sync-hosts-config",
72+
Usage: "Synchronizes hosts configurations",
73+
ArgsUsage: " ",
74+
Action: func(c *cli.Context) {
75+
err := syncInfrastructureHostsConfig(c)
76+
if err != nil {
77+
log.Fatal("Error: ", err)
78+
}
79+
},
80+
},
81+
},
82+
}
83+
return command
84+
}
85+
86+
// Lists all the hosts associated with the deployment
87+
func listInfrastructureHosts(c *cli.Context, w io.Writer) error {
88+
89+
var err error
90+
client.Photonclient, err = client.GetClient(c)
91+
if err != nil {
92+
return err
93+
}
94+
95+
hosts, err := client.Photonclient.InfraHosts.GetHosts()
96+
if err != nil {
97+
return err
98+
}
99+
100+
if utils.NeedsFormatting(c) {
101+
utils.FormatObjects(hosts, w, c)
102+
} else {
103+
err = printHostList(hosts.Items, os.Stdout, c)
104+
if err != nil {
105+
return err
106+
}
107+
}
108+
109+
return nil
110+
}
111+
112+
// Update the image datastores using the information carried in cli.Context.
113+
func updateInfrastructureImageDatastores(c *cli.Context) error {
114+
115+
datastores := c.String("datastores")
116+
117+
if !c.GlobalIsSet("non-interactive") {
118+
var err error
119+
datastores, err = askForInput("Datastores: ", datastores)
120+
if err != nil {
121+
return err
122+
}
123+
}
124+
125+
if len(datastores) == 0 {
126+
return fmt.Errorf("Please provide datastores using --datastores flag")
127+
}
128+
129+
imageDataStores := &photon.ImageDatastores{
130+
Items: regexp.MustCompile(`\s*,\s*`).Split(datastores, -1),
131+
}
132+
var err error
133+
client.Photonclient, err = client.GetClient(c)
134+
if err != nil {
135+
return err
136+
}
137+
138+
task, err := client.Photonclient.Infra.SetImageDatastores(imageDataStores)
139+
if err != nil {
140+
return err
141+
}
142+
143+
_, err = waitOnTaskOperation(task.ID, c)
144+
if err != nil {
145+
return err
146+
}
147+
148+
err = systemInfoJsonHelper(c, client.Photonclient)
149+
if err != nil {
150+
return err
151+
}
152+
153+
return nil
154+
}
155+
156+
// Synchronizes hosts configurations
157+
func syncInfrastructureHostsConfig(c *cli.Context) error {
158+
var err error
159+
if err != nil {
160+
return err
161+
}
162+
163+
client.Photonclient, err = client.GetClient(c)
164+
if err != nil {
165+
return err
166+
}
167+
168+
task, err := client.Photonclient.Infra.SyncHostsConfig()
169+
if err != nil {
170+
return err
171+
}
172+
173+
_, err = waitOnTaskOperation(task.ID, c)
174+
if err != nil {
175+
return err
176+
}
177+
178+
err = systemInfoJsonHelper(c, client.Photonclient)
179+
if err != nil {
180+
return err
181+
}
182+
183+
return nil
184+
}

photon/command/infrastructure_test.go

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// Copyright (c) 2016 VMware, Inc. All Rights Reserved.
2+
//
3+
// This product is licensed to you under the Apache License, Version 2.0 (the "License").
4+
// You may not use this product except in compliance with the License.
5+
//
6+
// This product may include a number of subcomponents with separate copyright notices and
7+
// license terms. Your use of these subcomponents is subject to the terms and conditions
8+
// of the subcomponent's license, as noted in the LICENSE file.
9+
10+
package command
11+
12+
import (
13+
"encoding/json"
14+
"flag"
15+
"net/http"
16+
"os"
17+
"testing"
18+
19+
"github.com/vmware/photon-controller-cli/photon/client"
20+
"github.com/vmware/photon-controller-cli/photon/mocks"
21+
22+
"github.com/urfave/cli"
23+
"github.com/vmware/photon-controller-go-sdk/photon"
24+
)
25+
26+
type MockHostsPage struct {
27+
Items []photon.Host `json:"items"`
28+
NextPageLink string `json:"nextPageLink"`
29+
PreviousPageLink string `json:"previousPageLink"`
30+
}
31+
32+
func TestListInfrastructureHosts(t *testing.T) {
33+
hostList := MockHostsPage{
34+
Items: []photon.Host{
35+
{
36+
Username: "u",
37+
Password: "p",
38+
Address: "testIP",
39+
Tags: []string{"CLOUD"},
40+
ID: "host-test-id",
41+
State: "COMPLETED",
42+
},
43+
},
44+
NextPageLink: "/fake-next-page-link",
45+
PreviousPageLink: "",
46+
}
47+
48+
response, err := json.Marshal(hostList)
49+
if err != nil {
50+
t.Error("Not expecting error serializing host list")
51+
}
52+
53+
server := mocks.NewTestServer()
54+
mocks.RegisterResponder(
55+
"GET",
56+
server.URL+rootUrl+"/infrastructure/hosts",
57+
mocks.CreateResponder(200, string(response[:])))
58+
59+
hostList = MockHostsPage{
60+
Items: []photon.Host{},
61+
NextPageLink: "",
62+
PreviousPageLink: "",
63+
}
64+
response, err = json.Marshal(hostList)
65+
if err != nil {
66+
t.Error("Not expecting error serializing expected taskLists")
67+
}
68+
69+
mocks.RegisterResponder(
70+
"GET",
71+
server.URL+"/fake-next-page-link",
72+
mocks.CreateResponder(200, string(response[:])))
73+
74+
defer server.Close()
75+
76+
mocks.Activate(true)
77+
httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
78+
client.Photonclient = photon.NewTestClient(server.URL, nil, httpClient)
79+
80+
set := flag.NewFlagSet("test", 0)
81+
err = set.Parse([]string{"1"})
82+
if err != nil {
83+
t.Error("Not expecting arguments parsing to fail")
84+
}
85+
cxt := cli.NewContext(nil, set, nil)
86+
err = listInfrastructureHosts(cxt, os.Stdout)
87+
if err != nil {
88+
t.Error("Not expecting deployment list hosts to fail")
89+
}
90+
}
91+
92+
func TestUpdateInfrastructureImageDatastores(t *testing.T) {
93+
deploymentId := "deployment1"
94+
queuedTask := &photon.Task{
95+
Operation: "UPDATE_IMAGE_DATASTORES",
96+
State: "QUEUED",
97+
Entity: photon.Entity{ID: "1"},
98+
}
99+
completedTask := photon.Task{
100+
ID: "task1",
101+
Operation: "UPDATE_IMAGE_DATASTORES",
102+
State: "COMPLETED",
103+
Entity: photon.Entity{ID: deploymentId},
104+
}
105+
response, err := json.Marshal(queuedTask)
106+
if err != nil {
107+
t.Error("Not expecting error when serializing tasks")
108+
}
109+
taskResponse, err := json.Marshal(completedTask)
110+
if err != nil {
111+
t.Error("Not expecting error during serializing expected completedTask")
112+
}
113+
114+
server := mocks.NewTestServer()
115+
mocks.RegisterResponder(
116+
"POST",
117+
server.URL+rootUrl+"/infrastructure/image-datastores",
118+
mocks.CreateResponder(200, string(response[:])))
119+
mocks.RegisterResponder(
120+
"GET",
121+
server.URL+rootUrl+"/tasks/"+queuedTask.ID,
122+
mocks.CreateResponder(200, string(taskResponse[:])))
123+
defer server.Close()
124+
125+
mocks.Activate(true)
126+
httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
127+
client.Photonclient = photon.NewTestClient(server.URL, nil, httpClient)
128+
129+
set := flag.NewFlagSet("test", 0)
130+
err = set.Parse([]string{deploymentId})
131+
if err != nil {
132+
t.Error(err)
133+
}
134+
set.String("datastores", "ds1,ds2", "blob")
135+
ctx := cli.NewContext(nil, set, nil)
136+
137+
err = updateInfrastructureImageDatastores(ctx)
138+
if err != nil {
139+
t.Error(err)
140+
}
141+
}
142+
143+
func TestSyncInfrastructureHostsConfig(t *testing.T) {
144+
queuedTask := &photon.Task{
145+
Operation: "SYNC_HOSTS_CONFIG",
146+
State: "QUEUED",
147+
Entity: photon.Entity{ID: "1"},
148+
}
149+
completedTask := &photon.Task{
150+
Operation: "SYNC_HOSTS_CONFIG",
151+
State: "COMPLETED",
152+
Entity: photon.Entity{ID: "1"},
153+
}
154+
155+
response, err := json.Marshal(queuedTask)
156+
if err != nil {
157+
t.Error("Not expecting error during serializing expected queuedTask")
158+
}
159+
taskResponse, err := json.Marshal(completedTask)
160+
if err != nil {
161+
t.Error("Not expecting error during serializing expected completedTask")
162+
}
163+
164+
server := mocks.NewTestServer()
165+
mocks.RegisterResponder(
166+
"POST",
167+
server.URL+rootUrl+"/infrastructure/sync-hosts-config",
168+
mocks.CreateResponder(200, string(response[:])))
169+
mocks.RegisterResponder(
170+
"GET",
171+
server.URL+rootUrl+"/tasks/"+queuedTask.ID,
172+
mocks.CreateResponder(200, string(taskResponse[:])))
173+
defer server.Close()
174+
175+
mocks.Activate(true)
176+
httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
177+
client.Photonclient = photon.NewTestClient(server.URL, nil, httpClient)
178+
179+
set := flag.NewFlagSet("test", 0)
180+
err = set.Parse([]string{queuedTask.Entity.ID})
181+
if err != nil {
182+
t.Error("Not expecting arguments parsing to fail")
183+
}
184+
cxt := cli.NewContext(nil, set, nil)
185+
186+
err = syncInfrastructureHostsConfig(cxt)
187+
if err != nil {
188+
t.Error(err)
189+
t.Error("Not expecting syncHostsConfig to fail")
190+
}
191+
}

0 commit comments

Comments
 (0)