-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use keystone to auth and service-discovery
- Loading branch information
Showing
123 changed files
with
1,281 additions
and
1,393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,97 @@ | ||
package selectel | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-retryablehttp" | ||
domainsV1 "github.com/selectel/domains-go/pkg/v1" | ||
"github.com/selectel/go-selvpcclient/v2/selvpcclient" | ||
"github.com/selectel/go-selvpcclient/v2/selvpcclient/quotamanager" | ||
"github.com/selectel/go-selvpcclient/v2/selvpcclient/resell" | ||
resellV2 "github.com/selectel/go-selvpcclient/v2/selvpcclient/resell/v2" | ||
"context" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/selectel/go-selvpcclient/v3/selvpcclient" | ||
) | ||
|
||
var ( | ||
cfgSingletone *Config | ||
once sync.Once | ||
) | ||
|
||
// Config contains all available configuration options. | ||
type Config struct { | ||
Token string | ||
Endpoint string | ||
ProjectID string | ||
Region string | ||
ProjectID string | ||
|
||
Context context.Context | ||
AuthURL string | ||
Username string | ||
Password string | ||
UserDomainName string | ||
DomainName string | ||
clientsCache map[string]*selvpcclient.Client | ||
lock sync.Mutex | ||
} | ||
|
||
// Validate performs config validation. | ||
func (c *Config) Validate() error { | ||
if c.Token == "" { | ||
return errors.New("token must be specified") | ||
} | ||
if c.Endpoint == "" { | ||
c.Endpoint = strings.Join([]string{resell.Endpoint, resellV2.APIVersion}, "/") | ||
} | ||
if c.Region != "" { | ||
if err := validateRegion(c.Region); err != nil { | ||
return err | ||
func getConfig(d *schema.ResourceData) (*Config, diag.Diagnostics) { | ||
var err error | ||
|
||
once.Do(func() { | ||
cfgSingletone = &Config{ | ||
Username: d.Get("username").(string), | ||
Password: d.Get("password").(string), | ||
DomainName: d.Get("domain_name").(string), | ||
} | ||
if v, ok := d.GetOk("auth_url"); ok { | ||
cfgSingletone.AuthURL = v.(string) | ||
} | ||
if v, ok := d.GetOk("user_domain_name"); ok { | ||
cfgSingletone.UserDomainName = v.(string) | ||
} | ||
if v, ok := d.GetOk("project_id"); ok { | ||
cfgSingletone.ProjectID = v.(string) | ||
} | ||
if v, ok := d.GetOk("region"); ok { | ||
cfgSingletone.Region = v.(string) | ||
} | ||
}) | ||
if err != nil { | ||
return nil, diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
return cfgSingletone, nil | ||
} | ||
|
||
func (c *Config) resellV2Client() *selvpcclient.ServiceClient { | ||
return resellV2.NewV2ResellClientWithEndpoint(c.Token, c.Endpoint) | ||
func (c *Config) GetSelVPCClient() (*selvpcclient.Client, error) { | ||
return c.GetSelVPCClientWithProjectScope("") | ||
} | ||
|
||
func (c *Config) domainsV1Client() *domainsV1.ServiceClient { | ||
domainsClient := domainsV1.NewDomainsClientV1WithDefaultEndpoint(c.Token) | ||
retryClient := retryablehttp.NewClient() | ||
retryClient.Logger = nil // Ignore retyablehttp client logs | ||
retryClient.RetryWaitMin = domainsV1DefaultRetryWaitMin | ||
retryClient.RetryWaitMax = domainsV1DefaultRetryWaitMax | ||
retryClient.RetryMax = domainsV1DefaultRetry | ||
domainsClient.HTTPClient = retryClient.StandardClient() | ||
func (c *Config) GetSelVPCClientWithProjectScope(projectID string) (*selvpcclient.Client, error) { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
|
||
return domainsClient | ||
} | ||
clientsCacheKey := fmt.Sprintf("client_%s", projectID) | ||
|
||
if client, ok := c.clientsCache[clientsCacheKey]; ok { | ||
return client, nil | ||
} | ||
|
||
opts := &selvpcclient.ClientOptions{ | ||
DomainName: c.DomainName, | ||
Username: c.Username, | ||
Password: c.Password, | ||
ProjectID: projectID, | ||
AuthURL: c.AuthURL, | ||
UserDomainName: c.UserDomainName, | ||
} | ||
|
||
client, err := selvpcclient.NewClient(opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if c.clientsCache == nil { | ||
c.clientsCache = map[string]*selvpcclient.Client{} | ||
} | ||
|
||
c.clientsCache[clientsCacheKey] = client | ||
|
||
func (c *Config) quotaManagerRegionalClient( | ||
identity quotamanager.IdentityManagerInterface, | ||
) *quotamanager.QuotaRegionalClient { | ||
return quotamanager.NewQuotaRegionalClient(selvpcclient.NewHTTPClient(), identity) | ||
return client, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package selectel | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
v1 "github.com/selectel/craas-go/pkg" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func newCRaaSTestClient(rs *terraform.ResourceState, testAccProvider *schema.Provider) (*v1.ServiceClient, error) { | ||
config := testAccProvider.Meta().(*Config) | ||
|
||
var projectID string | ||
|
||
if id, ok := rs.Primary.Attributes["project_id"]; ok { | ||
projectID = id | ||
} | ||
|
||
selvpcClient, err := config.GetSelVPCClientWithProjectScope(projectID) | ||
if err != nil { | ||
return nil, fmt.Errorf("can't get selvpc client for craas acc tests: %w", err) | ||
} | ||
|
||
craasEndpoint, err := getEndpointForCRaaS(selvpcClient) | ||
if err != nil { | ||
return nil, fmt.Errorf("can't get endpoint for craas acc tests: %w", err) | ||
} | ||
|
||
craasClient := v1.NewCRaaSClientV1(selvpcClient.GetXAuthToken(), craasEndpoint) | ||
|
||
return craasClient, nil | ||
} | ||
|
||
func TestGetHostNameForCRaaS(t *testing.T) { | ||
expected := "https://cr.selcloud.ru" | ||
actual, err := getHostNameForCRaaS("https://cr.selcloud.ru/api/v1") | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, actual) | ||
} |
Oops, something went wrong.