Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #20 from PatrykMatyjasek/pm-add-support-api-v3
Browse files Browse the repository at this point in the history
Add support for authentication API v3
  • Loading branch information
PatrykMatyjasek authored Apr 4, 2017
2 parents da4f2e7 + 0bd9903 commit af2b6c2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ User need to provide following parameters in configuration for collector:
- `"tenant"` - name of the tenant, this is required if not provided in global config
- `"user"` - user name which has access to tenant
- `"password"` - user password
If you're using authentication API in v3 you need to set one of those two configuration options:
- `"domain_name"` - domain name
- `"domain_id"` - domain name

See example task manifest in [examples/task] (examples/tasks/task.json).

Expand Down
19 changes: 14 additions & 5 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

const (
name = "glance"
version = 3
version = 4
plgtype = plugin.CollectorPluginType
vendor = "intel"
fs = "openstack"
Expand Down Expand Up @@ -83,7 +83,8 @@ func (c *collector) GetMetricTypes(cfg plugin.ConfigType) ([]plugin.MetricType,
// CollectMetrics returns list of requested metric values
// It returns error in case retrieval was not successful
func (c *collector) CollectMetrics(metricTypes []plugin.MetricType) ([]plugin.MetricType, error) {
//allImages := map[string]types.Images{}
domain_name := ""
domain_id := ""

// get credentials and endpoint from configuration
items, err := config.GetConfigItems(metricTypes[0], "endpoint", "tenant", "user", "password")
Expand All @@ -95,8 +96,16 @@ func (c *collector) CollectMetrics(metricTypes []plugin.MetricType) ([]plugin.Me
tenant := items["tenant"].(string)
user := items["user"].(string)
password := items["password"].(string)
dom_name, _ := config.GetConfigItem(metricTypes[0], "domain_name")
dom_id, _ := config.GetConfigItem(metricTypes[0], "domain_id")
if dom_name != nil {
domain_name = dom_name.(string)
}
if dom_id != nil {
domain_id = dom_id.(string)
}

if err := c.authenticate(endpoint, tenant, user, password); err != nil {
if err := c.authenticate(endpoint, tenant, user, password, domain_name, domain_id); err != nil {
return nil, err
}

Expand Down Expand Up @@ -162,9 +171,9 @@ type collector struct {
providers map[string]*gophercloud.ProviderClient
}

func (c *collector) authenticate(endpoint, tenant, user, password string) error {
func (c *collector) authenticate(endpoint, tenant, user, password, domain_name, domain_id string) error {
if _, found := c.providers[tenant]; !found {
provider, err := openstackintel.Authenticate(endpoint, user, password, tenant)
provider, err := openstackintel.Authenticate(endpoint, user, password, tenant, domain_name, domain_id)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions openstack/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Common struct{}
func (c Common) GetTenants(endpoint, user, password string) ([]types.Tenant, error) {
tnts := []types.Tenant{}

provider, err := Authenticate(endpoint, user, password, "")
provider, err := Authenticate(endpoint, user, password, "", "", "")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -108,14 +108,20 @@ func (c Common) GetApiVersions(provider *gophercloud.ProviderClient) ([]types.Ap

// Authenticate is used to authenticate user for given tenant. Request is send to provided Keystone endpoint
// Returns authenticated provider client, which is used as a base for service clients.
func Authenticate(endpoint, user, password, tenant string) (*gophercloud.ProviderClient, error) {
func Authenticate(endpoint, user, password, tenant, domain_name, domain_id string) (*gophercloud.ProviderClient, error) {
authOpts := gophercloud.AuthOptions{
IdentityEndpoint: endpoint,
Username: user,
Password: password,
TenantName: tenant,
AllowReauth: true,
}
if domain_name != "" && domain_id == "" {
authOpts.DomainName = domain_name
}
if domain_id != "" && domain_name == "" {
authOpts.DomainID = domain_id
}

provider, err := openstack.AuthenticatedClient(authOpts)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion openstack/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (s *CommonSuite) TestGetAPI() {
Convey("Given api versions are requested", s.T(), func() {
c := Common{}
Convey("When GetAPIVersions is called", func() {
provider, err := Authenticate(th.Endpoint(), "me", "secret", "tenant")
provider, err := Authenticate(th.Endpoint(), "me", "secret", "tenant", "", "")
th.AssertNoErr(s.T(), err)
th.CheckEquals(s.T(), s.Token, provider.TokenID)

Expand Down
2 changes: 1 addition & 1 deletion openstack/v1/glance/glance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *GlanceV1Suite) TestGetImages() {
Convey("Given Glance images are requested", s.T(), func() {

Convey("When authentication is required", func() {
provider, err := openstackintel.Authenticate(th.Endpoint(), "me", "secret", "tenant")
provider, err := openstackintel.Authenticate(th.Endpoint(), "me", "secret", "tenant", "", "")
th.AssertNoErr(s.T(), err)
th.CheckEquals(s.T(), s.Token, provider.TokenID)

Expand Down
2 changes: 1 addition & 1 deletion openstack/v2/glance/glance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *GlanceV2Suite) TestGetImages() {
Convey("Given Glance images are requested", s.T(), func() {

Convey("When authentication is required", func() {
provider, err := openstackintel.Authenticate(th.Endpoint(), "me", "secret", "tenant")
provider, err := openstackintel.Authenticate(th.Endpoint(), "me", "secret", "tenant", "", "")
th.AssertNoErr(s.T(), err)
th.CheckEquals(s.T(), s.Token, provider.TokenID)

Expand Down

0 comments on commit af2b6c2

Please sign in to comment.