Beispiel #1
0
// initIdentityClient initializes openstack api using
// gophercloud which handles auth tokens keeping api calls
// simpler. Currently it uses environment variables for
// authenticating with openstack identity.
func initIdentityClient() (*gophercloud.ServiceClient, error) {
	opts, err := openstack.AuthOptionsFromEnv()
	if err != nil {
		log.Println("Error fetching openstack env vars: ", err)
		return nil, err
	}
	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		log.Println("Error authenticating with openstack: ", err)
		return nil, err
	}
	return openstack.NewIdentityV2(provider), nil
}
Beispiel #2
0
// initComputeClient initializes openstack api using
// gophercloud which handles auth tokens keeping api calls
// simpler. Currently it uses environment variables for
// authenticating with openstack identity.
func initComputeClient() (*gophercloud.ServiceClient, error) {
	opts, err := openstack.AuthOptionsFromEnv()
	if err != nil {
		log.Println("Error fetching openstack env vars: ", err)
		return nil, err
	}
	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		log.Println("Error authenticating with openstack: ", err)
		return nil, err
	}
	return openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
		Name:   "compute",
		Region: os.Getenv("OS_REGION_NAME"),
	})
}
Beispiel #3
0
// GetTenantUUID returns openstack tenant UUID
// corresponding to the given tenantName.
func GetTenantUUID(tenantName string) (string, error) {
	var uuid string

	c, err := getIdentityClient()
	if err != nil {
		log.Println("Error getting Identity Client: ", err)
		return "", err
	}

	opts := tenants.ListOpts{Limit: 20}
	pager := tenants.List(c, &opts)
	// brute force the whole tenant list to get the name?
	pager.EachPage(
		func(page pagination.Page) (bool, error) {
			tenantList, _ := tenants.ExtractTenants(page)
			for _, t := range tenantList {
				// "t" is tenants.Tenant
				if t.Name == tenantName {
					uuid = t.ID
					// stop iterating and return tenant.Name
					return false, nil
				}
			}
			return true, nil
		},
	)

	if uuid == "" {
		log.Printf("Tenant (Name: %s) not found.\n", tenantName)
		return "", util.ErrTenantNotFound
	}

	return uuid, nil
}
Beispiel #4
0
// TenantExists returns true/false depending on
// openstack tenant name or uuid exists.
func TenantExists(name string) bool {
	var tenant bool

	c, err := getIdentityClient()
	if err != nil {
		log.Println("Error getting Identity Client: ", err)
		return false
	}

	opts := tenants.ListOpts{Limit: 20}
	pager := tenants.List(c, &opts)
	// brute force the whole tenant list to get tenant details?
	pager.EachPage(
		func(page pagination.Page) (bool, error) {
			tenantList, _ := tenants.ExtractTenants(page)
			for _, t := range tenantList {
				// "t" is tenants.Tenant
				if t.ID == name || t.Name == name {
					tenant = true
					// stop iterating and return tenant
					return false, nil
				}
			}
			return true, nil
		},
	)
	return tenant
}
Beispiel #5
0
func getNetworkClient() (*gophercloud.ServiceClient, error) {
	var err error
	if networkClient == nil {
		networkClient, err = initNetworkClient()
		if err != nil {
			log.Println("Error: ", err)
			return nil, err
		}
	}
	return networkClient, nil
}
Beispiel #6
0
func getComputeClient() (*gophercloud.ServiceClient, error) {
	var err error
	if computeClient == nil {
		computeClient, err = initComputeClient()
		if err != nil {
			log.Println("Error: ", err)
			return nil, err
		}
	}
	return computeClient, nil
}
Beispiel #7
0
func getIdentityClient() (*gophercloud.ServiceClient, error) {
	var err error
	if identityClient == nil {
		identityClient, err = initIdentityClient()
		if err != nil {
			log.Println("Error: ", err)
			return nil, err
		}
	}
	return identityClient, nil
}
Beispiel #8
0
// initConfig reads in config file and ENV variables if set.
func initConfig() {
	// https://github.com/spf13/viper/commit/5619c0 changes the behaviour
	// of SetConfigFile and SetConfigName, thus SetConfigName should come
	// before SetConfigFile.
	config.SetConfigName(".romana") // name of config file (without extension)
	config.AddConfigPath("$HOME")   // adding home directory as first search path

	if cfgFile != "" { // enable ability to specify config file via flag
		config.SetConfigFile(cfgFile)
	}

	config.AutomaticEnv() // read in environment variables that match

	// If a config file is found, read it in.
	err := config.ReadInConfig()
	setLogOutput()
	if err != nil {
		log.Println("Error using config file:", config.ConfigFileUsed())
	} else {
		log.Println("Using config file:", config.ConfigFileUsed())
	}
}
Beispiel #9
0
// Execute adds all child commands to the root command and sets
// flags appropriately. This is called by main.main(). It only
// needs to happen once to the rootCmd. Here commands/subcommand
// mapping is added where control is passed around from main()
// to commands/subcommands evoked.
func Execute() {
	if err := RootCmd.Execute(); err != nil {
		log.Println(err)
		os.Exit(-1)
	}
}