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 }
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 }
func cmdUseVDC(cmd *cobra.Command, args []string) { initConfig(cmd, "goair_vcd", true, map[string]FlagValue{ "vdchref": {vdchref, true, false, ""}, "vdcname": {vdcname, true, false, "vdchref"}, }) client, err := authenticatevcd(false) if err != nil { log.Fatalf("failed authenticating: %s", err) } links, err := govcloudair.GetOrgVdc(client, &client.VCDORGHREF) useLink := types.Link{} for _, link := range links { if (viper.GetString("vdchref") != "" && link.HREF == viper.GetString("vdchref")) || (viper.GetString("vdcname") != "" && link.Name == viper.GetString("vdcname")) { useLink = link break } } if useLink.Name != "" { err = clue.EncodeGobFile("goair_compute", clue.UseValue{ VarMap: map[string]string{ "vdchref": useLink.HREF, "VCDToken": client.VCDToken, "VCDAuthHeader": client.VCDAuthHeader, "orghref": client.VCDORGHREF.String(), }, }) yamlOutput, err := yaml.Marshal(&useLink) if err != nil { log.Fatalf("error marshaling: %s", err) } fmt.Println(string(yamlOutput)) } else { log.Fatalf("Failed to find VDC %v%v", vdcname, vdchref) } }