Beispiel #1
0
func authenticate(force bool) (client *govcloudair.Client, err error) {
	client, err = govcloudair.NewClient()
	if err != nil {
		return client, fmt.Errorf("error with NewClient: %s", err)
	}

	getValue := clue.GetValue{}
	if err := clue.DecodeGobFile("goair_client", &getValue); err != nil {
		return &govcloudair.Client{}, fmt.Errorf("Problem with client decodeGobFile: %v", err)
	}

	if force || (getValue.VarMap["VAToken"] == nil && getValue.VarMap["VCDToken"] == nil) {
		err = client.AuthenticateOD("", "")
		if err != nil {
			return client, fmt.Errorf("error Authenticating: %s", err)
		}

		err = clue.EncodeGobFile("goair_client", clue.UseValue{
			VarMap: map[string]string{
				"VAToken": client.VAToken,
			},
		})
	} else {
		if getValue.VarMap["VAToken"] != nil {
			client.VAToken = *getValue.VarMap["VAToken"]
		}
	}

	return client, err
}
Beispiel #2
0
func authenticatevcd(force bool) (client *govcloudair.Client, err error) {
	client, err = govcloudair.NewClient()
	if err != nil {
		return client, fmt.Errorf("error with NewClient: %s", err)
	}

	getValue := clue.GetValue{}
	if err := clue.DecodeGobFile("goair_client", &getValue); err != nil {
		return &govcloudair.Client{}, fmt.Errorf("Problem with client decodeGobFile: %v", err)
	}

	var (
		orgname    = viper.GetString("orgname")
		sessionuri = viper.GetString("sessionuri")
	)

	if force || getValue.VarMap["VCDToken"] == nil {
		instanceAttributes := vcatypes.InstanceAttributes{OrgName: orgname, SessionURI: sessionuri}
		err = client.GetBackendAuthOD(instanceAttributes)
		if err != nil {
			return client, fmt.Errorf("error Authenticating: %s", err)
		}

		err = clue.EncodeGobFile("goair_client", clue.UseValue{
			VarMap: map[string]string{
				"VCDToken":      client.VCDToken,
				"VCDORGHREF":    client.VCDORGHREF.String(),
				"VCDAuthHeader": client.VCDAuthHeader,
			},
		})
	} else {
		client.VCDToken = *getValue.VarMap["VCDToken"]

		orgUri, err := url.ParseRequestURI(*getValue.VarMap["VCDORGHREF"])
		if err != nil {
			return client, fmt.Errorf("cannot parse endpoint coming from VCDORGHREF")
		}

		client.VCDORGHREF = *orgUri
		client.VCDAuthHeader = *getValue.VarMap["VCDAuthHeader"]
	}

	return client, err
}
Beispiel #3
0
func setGobValues(cmd *cobra.Command, suffix string, field string) (err error) {
	getValue := clue.GetValue{}
	if err := clue.DecodeGobFile(suffix, &getValue); err != nil {
		return fmt.Errorf("Problem with decodeGobFile: %v", err)
	}

	if os.Getenv("VCLOUDAIR_SHOW_GOB") == "true" {
		for key, value := range getValue.VarMap {
			fmt.Printf("%v: %v\n", key, *value)
		}
		fmt.Printf("%+v\n", getValue.VarMap)
		fmt.Println()
	}

	for key := range getValue.VarMap {
		lowerKey := strings.ToLower(key)
		if field != "" && field != lowerKey {
			continue
		}
		viper.Set(lowerKey, *getValue.VarMap[key])
	}
	return
}