func TestTargetShow(t *testing.T) {
	configOri, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	configRead, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	set := flag.NewFlagSet("test", 0)
	cxt := cli.NewContext(nil, set, nil)
	err = showEndpoint(cxt)
	if err != nil {
		t.Error("Not expecting error showing endpoint")
	}

	if configRead.CloudTarget != configOri.CloudTarget {
		t.Error("Endpoint should not have changed from show endpoint")
	}

	err = cf.SaveConfig(configOri)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}
}
Example #2
0
// Read config from config file, change target and then write back to file
// Also check if the target is reachable securely
func setEndpoint(c *cli.Context) error {
	err := checkArgCount(c, 1)
	if err != nil {
		return err
	}
	endpoint := c.Args()[0]
	noCertCheck := c.Bool("nocertcheck")

	config, err := cf.LoadConfig()
	if err != nil {
		return err
	}

	config.CloudTarget = endpoint
	config.IgnoreCertificate = noCertCheck

	err = cf.SaveConfig(config)
	if err != nil {
		return err
	}

	err = configureServerCerts(endpoint, noCertCheck, c)
	if err != nil {
		return err
	}

	fmt.Printf("API target set to '%s'\n", endpoint)

	err = clearConfigTenant("")
	if err != nil {
		return err
	}

	return err
}
Example #3
0
// Handles show-login-token, which shows the current login token, if any
func showLoginTokenWriter(c *cli.Context, w io.Writer, config *configuration.Configuration) error {
	err := checkArgCount(c, 0)
	if err != nil {
		return err
	}

	if config == nil {
		config, err = configuration.LoadConfig()
		if err != nil {
			return err
		}
	}

	if config.Token == "" {
		err = fmt.Errorf("No login token available")
		return err
	}
	if !c.GlobalIsSet("non-interactive") {
		raw := c.Bool("raw")
		if raw {
			dumpTokenDetailsRaw(w, "Login Access Token", config.Token)
		} else {
			dumpTokenDetails(w, "Login Access Token", config.Token)
		}
	} else {
		fmt.Fprintf(w, "%s\n", config.Token)
	}
	return nil
}
func TestGet(t *testing.T) {
	configOri, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	// test Get when config file doesn't exist
	err = cf.RemoveConfigFile()
	if err != nil {
		t.Error("Not expecting error removing config file")
	}
	_, err = get()
	if err == nil {
		t.Error("Expected to receive error trying to get client when config file does not exist")
	}

	// test GetClient with valid endpoint in config file
	endpoint := "http://localhost:9080"
	testGetEndpoint(t, endpoint, false, true)

	endpoint = "https://172.31.253.66:443"
	//test Get for a https endpoint with secure endpoint = true & skipping verify = true
	testGetEndpoint(t, endpoint, true, true)

	//test Get for a https endpoint with secure endpoint = true & skipping verify = false
	testGetEndpoint(t, endpoint, true, false)

	//Restore the original configuration
	err = cf.SaveConfig(configOri)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}
}
Example #5
0
// Read from local config file and create a new photon client using target
func get() (*photon.Client, error) {
	config, err := cf.LoadConfig()
	if err != nil {
		return nil, err
	}

	return NewClient(config)
}
Example #6
0
// Shows set endpoint
func showEndpoint(c *cli.Context) error {
	err := checkArgCount(c, 0)
	if err != nil {
		return err
	}
	config, err := cf.LoadConfig()
	if err != nil {
		return err
	}

	if len(config.CloudTarget) == 0 {
		fmt.Printf("No API target set\n")
	} else {
		fmt.Printf("Current API target is '%s'\n", config.CloudTarget)
	}
	return nil
}
Example #7
0
// Verifies and gets tenant name and id for commands specifying tenant
// Returns tenant in config file if name is empty
func verifyTenant(name string) (*cf.TenantConfiguration, error) {
	if len(name) != 0 {
		tenantID, err := findTenantID(name)
		if len(tenantID) == 0 || err != nil {
			return nil, err
		}
		return &cf.TenantConfiguration{Name: name, ID: tenantID}, nil
	}

	config, err := cf.LoadConfig()
	if err != nil {
		return nil, err
	}
	if config.Tenant == nil {
		return nil, fmt.Errorf("Error: Set tenant first using 'tenant set <name>' or '-t <name>' option")
	}

	return config.Tenant, nil
}
Example #8
0
// Clears the project in the config if it has a matching id
func clearConfigProject(id string) error {
	config, err := cf.LoadConfig()
	if err != nil {
		return err
	}

	if config == nil || config.Project == nil {
		return nil
	}

	if len(id) == 0 || config.Project.ID == id {
		config.Project = nil
		err = cf.SaveConfig(config)
		if err != nil {
			return err
		}
	}
	return nil
}
Example #9
0
// Verifies and gets project name and id for commands specifying project
// Returns project in config file if name is empty
func verifyProject(tenantID string, name string) (*cf.ProjectConfiguration, error) {
	if len(name) != 0 {
		project, err := findProject(tenantID, name)
		if err != nil {
			return nil, err
		}
		return &cf.ProjectConfiguration{Name: name, ID: project.ID}, nil
	}

	config, err := cf.LoadConfig()
	if err != nil {
		return nil, err
	}
	if config.Project == nil {
		return nil, fmt.Errorf("Error: Set project first using 'project set <name>' or '-p <name>' option")
	}

	return config.Project, nil
}
Example #10
0
// Remove token from the config file
func logout(c *cli.Context) error {
	err := checkArgCount(c, 0)
	if err != nil {
		return err
	}
	config, err := cf.LoadConfig()
	if err != nil {
		return err
	}

	config.Token = ""

	err = cf.SaveConfig(config)
	if err != nil {
		return err
	}

	fmt.Println("Token removed from config file")

	return nil
}
Example #11
0
// Overwrites the tenant in the config file
func setTenant(c *cli.Context) error {
	err := checkArgCount(c, 1)
	if err != nil {
		return err
	}
	name := c.Args().First()

	client.Esxclient, err = client.GetClient(c)
	if err != nil {
		return err
	}

	// Ensure tenant exists
	id, err := findTenantID(name)
	if len(id) == 0 || err != nil {
		return err
	}

	config, err := cf.LoadConfig()
	if err != nil {
		return err
	}

	config.Tenant = &cf.TenantConfiguration{Name: name, ID: id}
	err = cf.SaveConfig(config)
	if err != nil {
		return err
	}

	err = clearConfigProject("")
	if err != nil {
		return err
	}

	if !c.GlobalIsSet("non-interactive") {
		fmt.Printf("Tenant set to '%s'\n", name)
	}
	return nil
}
Example #12
0
// Set project name and id to config file
// Returns an error if one occurred
func setProject(c *cli.Context) error {
	err := checkArgCount(c, 1)
	if err != nil {
		return err
	}
	name := c.Args().First()

	client.Esxclient, err = client.GetClient(c)
	if err != nil {
		return err
	}

	config, err := cf.LoadConfig()
	if err != nil {
		return err
	}

	if config == nil || config.Tenant == nil {
		return fmt.Errorf("Error: Set tenant first using 'tenant set <name>' or '-t <name>' option")
	}

	project, err := findProject(config.Tenant.ID, name)
	if err != nil {
		return err
	}

	config.Project = &cf.ProjectConfiguration{Name: project.Name, ID: project.ID}
	err = cf.SaveConfig(config)
	if err != nil {
		return err
	}

	if !c.GlobalIsSet("non-interactive") {
		fmt.Printf("Project set to '%s'\n", name)
	}

	return nil
}
Example #13
0
// Sends a get project task to client based on the config file
// Returns an error if one occurred
func getProject(c *cli.Context, w io.Writer) error {
	err := checkArgCount(c, 0)
	if err != nil {
		return err
	}
	config, err := cf.LoadConfig()
	if err != nil {
		return err
	}

	project := config.Project
	if project == nil {
		return fmt.Errorf("Error: No Project selected\n")
	}

	if c.GlobalIsSet("non-interactive") {
		fmt.Printf("%s\t%s\n", project.ID, project.Name)
	} else if utils.NeedsFormatting(c) {
		utils.FormatObject(project, w, c)
	} else {
		fmt.Printf("Current project is '%s' with ID %s\n", project.ID, project.Name)
	}
	return nil
}
Example #14
0
// Outputs the set tenant otherwise informs user it is not set
func getTenant(c *cli.Context, w io.Writer) error {
	err := checkArgCount(c, 0)
	if err != nil {
		return err
	}
	config, err := cf.LoadConfig()
	if err != nil {
		return err
	}

	tenant := config.Tenant
	if tenant == nil {
		fmt.Printf("No tenant selected\n")
	} else {
		if c.GlobalIsSet("non-interactive") {
			fmt.Printf("%s\t%s\n", tenant.ID, tenant.Name)
		} else if utils.NeedsFormatting(c) {
			utils.FormatObject(tenant, w, c)
		} else {
			fmt.Printf("Current tenant is '%s' with ID %s\n", tenant.Name, tenant.ID)
		}
	}
	return nil
}
Example #15
0
func printDetail() error {
	config, err := cf.LoadConfig()
	if err != nil {
		return err
	}

	if config != nil {
		fmt.Printf("Target: '%s'\n", Esxclient.Endpoint)

		if config.Tenant == nil {
			fmt.Printf("Tenant: <not-set> \n")
		} else {
			fmt.Printf("Tenant: '%s'\n", config.Tenant.Name)
		}

		if config.Project == nil {
			fmt.Printf("Project: <not-set> \n")
		} else {
			fmt.Printf("Project: '%s'\n", config.Project.Name)
		}
	}
	fmt.Printf("\n")
	return nil
}
Example #16
0
// Store token in the config file
func login(c *cli.Context) error {
	err := checkArgCount(c, 0)
	if err != nil {
		return err
	}

	username := c.String("username")
	password := c.String("password")
	token := c.String("access_token")

	if !c.GlobalIsSet("non-interactive") && len(token) == 0 {
		username, err = askForInput("User name (username@tenant): ", username)
		if err != nil {
			return err
		}
		if len(password) == 0 {
			fmt.Printf("Password:"******"\n")
		}
	}

	if len(token) == 0 && (len(username) == 0 || len(password) == 0) {
		return fmt.Errorf("Please provide either a token or username/password")
	}

	config, err := cf.LoadConfig()
	if err != nil {
		return err
	}

	if len(token) > 0 {
		config.Token = token

	} else {
		client.Esxclient, err = client.GetClient(c)
		if err != nil {
			return err
		}

		options, err := client.Esxclient.Auth.GetTokensByPassword(username, password)
		if err != nil {
			return err
		}

		config.Token = options.AccessToken
	}

	err = cf.SaveConfig(config)
	if err != nil {
		return err
	}

	fmt.Println("Token stored in config file")

	return nil
}
func TestSetGetProject(t *testing.T) {
	configOri, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	projectListStruct := photon.ProjectList{
		Items: []photon.ProjectCompact{
			{
				Name: "fake_project_name",
				ID:   "fake_project_ID",
				ResourceTicket: photon.ProjectTicket{
					Limits: []photon.QuotaLineItem{{Key: "vm.test1", Value: 1, Unit: "B"}},
					Usage:  []photon.QuotaLineItem{{Key: "vm.test1", Value: 0, Unit: "B"}},
				},
			},
		},
	}
	listResponse, err := json.Marshal(projectListStruct)
	if err != nil {
		t.Error("Not expecting error serializaing expected projectLists")
	}

	mocks.RegisterResponder(
		"GET",
		server.URL+"/tenants/"+"fake_tenant_ID"+"/projects",
		mocks.CreateResponder(200, string(listResponse[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tenants/"+"fake_tenant_ID"+"/projects?name="+"fake_project_name",
		mocks.CreateResponder(200, string(listResponse[:])))

	config := &cf.Configuration{
		Tenant: &cf.TenantConfiguration{Name: "fake_tenant_name", ID: "fake_tenant_ID"},
	}
	err = cf.SaveConfig(config)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"fake_project_name"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)

	err = setProject(cxt)
	if err != nil {
		t.Error("Not expecting error setting project: " + err.Error())
	}

	set = flag.NewFlagSet("test", 0)
	cxt = cli.NewContext(nil, set, nil)

	err = getProject(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error showing project: " + err.Error())
	}

	err = cf.SaveConfig(configOri)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}
}
func TestSetTenant(t *testing.T) {
	configRead, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	initialEndpoint := configRead.CloudTarget

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"errorname"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)

	err = setTenant(cxt)
	if err == nil {
		t.Error("Expecting error should not set tenant")
	}

	tenant := &cf.TenantConfiguration{Name: "testname", ID: "1"}
	err = set.Parse([]string{"testname"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt = cli.NewContext(nil, set, nil)

	err = setTenant(cxt)
	if err != nil {
		t.Error("Not expecting setting tenant to fail")
	}

	configRead, err = cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if !reflect.DeepEqual(configRead.Tenant, tenant) {
		t.Error("Tenant in config does not match what was to be written")
	}

	tenantOverwrite := &cf.TenantConfiguration{Name: "secondname", ID: "2"}
	err = set.Parse([]string{"secondname"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt = cli.NewContext(nil, set, nil)

	err = setTenant(cxt)
	if err != nil {
		t.Error("Not expecting setting tenant to fail")
	}

	configRead, err = cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if !reflect.DeepEqual(configRead.Tenant, tenantOverwrite) {
		t.Error("Tenant in config does not match what was to be written")
	}

	if configRead.CloudTarget != initialEndpoint {
		t.Error("Cloud target should not have been modified while changing tenant")
	}
}
func TestSetTenantAfterDelete(t *testing.T) {
	configRead, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	tenant := &cf.TenantConfiguration{Name: "testname", ID: "1"}

	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"testname"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt := cli.NewContext(nil, set, nil)

	err = setTenant(cxt)
	if err != nil {
		t.Error("Not expecting setting tenant to fail")
	}

	configRead, err = cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if !reflect.DeepEqual(configRead.Tenant, tenant) {
		t.Error("Tenant in config does not match what was to be written")
	}

	completedTask := &photon.Task{
		Operation: "DELETE_TENANT",
		State:     "COMPLETED",
		Entity:    photon.Entity{ID: "1"},
	}
	response, err := json.Marshal(completedTask)
	if err != nil {
		t.Error("Not expecting error serializaing expected queuedTask")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"DELETE",
		server.URL+"/tenants/"+completedTask.Entity.ID,
		mocks.CreateResponder(200, string(response[:])))
	mocks.RegisterResponder(
		"GET",
		server.URL+"/tasks/"+completedTask.ID,
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	err = set.Parse([]string{"1"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	cxt = cli.NewContext(nil, set, nil)

	err = deleteTenant(cxt)
	if err != nil {
		t.Error("Not expecting delete Tenant to fail", err)
	}

	configRead, err = cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.Tenant != nil {
		t.Error("Tenant in config does not match what was to be written")
	}
}
Example #20
0
func TestLogin(t *testing.T) {
	var token string

	configOri, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	// test Login when config file does not exist
	err = cf.RemoveConfigFile()
	if err != nil {
		t.Error("Not expecting error removing config file")
	}

	token = "token"
	set := flag.NewFlagSet("test", 0)
	set.String("access_token", token, "")
	cxt := cli.NewContext(nil, set, nil)
	err = login(cxt)
	if err != nil {
		t.Error("Not expecting error when logging in")
	}

	configRead, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.Token != token {
		t.Error("Token read from file not match what's written to file")
	}

	// test Login when overwriting existing endpoint
	configExpected := &cf.Configuration{
		CloudTarget: "test-login",
		Token:       "test-login",
	}
	err = cf.SaveConfig(configExpected)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}

	token = "token-overwrite"
	set = flag.NewFlagSet("test", 0)
	set.String("access_token", token, "")
	cxt = cli.NewContext(nil, set, nil)
	err = login(cxt)
	if err != nil {
		t.Error("Not expecting error when overwritting token in file")
	}

	configRead, err = cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.Token != token {
		t.Error("Token read from file not match what's written to file")
	}

	configRead.Token = configExpected.Token
	if *configRead != *configExpected {
		t.Error("Other configurations changed when setting only token")
	}

	err = cf.SaveConfig(configOri)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}
}
Example #21
0
func TestLogout(t *testing.T) {
	configOri, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	// test Logout when config file does not exist
	err = cf.RemoveConfigFile()
	if err != nil {
		t.Error("Not expecting error removing config file")
	}
	set := flag.NewFlagSet("test", 0)
	cxt := cli.NewContext(nil, set, nil)
	err = logout(cxt)
	if err != nil {
		t.Error("Not expecting error when logging out")
	}

	configRead, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.Token != "" {
		t.Error("Token expected to be empty after logout")
	}

	// test Logout when config file exists
	configExpected := &cf.Configuration{
		CloudTarget: "test-logout",
		Token:       "test-logout",
	}
	err = cf.SaveConfig(configExpected)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}

	err = logout(cxt)
	if err != nil {
		t.Error("Not expecting error when logging out")
	}

	configRead, err = cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.Token != "" {
		t.Error("Token expected to be empty after logout")
	}

	configRead.Token = configExpected.Token
	if *configRead != *configExpected {
		t.Error("Other configurations changed when removing only token")
	}

	err = cf.SaveConfig(configOri)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}
}
Example #22
0
func TestSetEndpoint(t *testing.T) {
	var endpoint string

	configOri, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	// test SetEndpoint when config file does not exist
	err = cf.RemoveConfigFile()
	if err != nil {
		t.Error("Not expecting error removing config file")
	}

	endpoint = "endpoint"
	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"endpoint"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	set.Bool("nocertcheck", true, "")
	cxt := cli.NewContext(nil, set, nil)
	err = setEndpoint(cxt)
	if err != nil {
		t.Error("Not expecting error when setting endpoint")
	}

	configRead, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.CloudTarget != endpoint {
		t.Error("Endpoint read from file not match what's written to file")
	}

	// test SetEndpoint when overwriting existing endpoint
	configExpected := &cf.Configuration{
		CloudTarget:       "test-setendpoint",
		Token:             "test-setendpoint",
		IgnoreCertificate: true,
	}
	err = cf.SaveConfig(configExpected)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}

	endpoint = "endpoint-overwrite"
	set = flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"endpoint-overwrite"})
	if err != nil {
		t.Error("Not expecting arguments parsign to fail")
	}
	set.Bool("nocertcheck", true, "")
	cxt = cli.NewContext(nil, set, nil)
	err = setEndpoint(cxt)
	if err != nil {
		t.Error("Not expecting error when overwritting endpoint in file")
	}

	configRead, err = cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.CloudTarget != endpoint {
		t.Error("Endpoint read from file not match what's written to file")
	}

	configRead.CloudTarget = configExpected.CloudTarget
	if *configRead != *configExpected {
		t.Error("Other configurations changed when setting only cloudtarget")
	}

	err = cf.SaveConfig(configOri)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}
}