-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: internal directories and files
- Loading branch information
Showing
30 changed files
with
437 additions
and
266 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
apiclient "github.com/orange-cloudavenue/cloudavenue-sdk-go" | ||
) | ||
|
||
var ( | ||
ErrAuthFailed = errors.New("authentication error") | ||
ErrTokenEmpty = errors.New("token is empty") | ||
) | ||
|
||
type CloudAvenue struct { | ||
User string | ||
Org string | ||
Password string | ||
URL string | ||
TerraformVersion string | ||
CloudAvenueVersion string | ||
APIClient *apiclient.APIClient | ||
Auth context.Context | ||
} | ||
|
||
// New creates a new CloudAvenue client. | ||
func (c *CloudAvenue) New() (*CloudAvenue, error) { | ||
auth := c.createBasicAuthContext() | ||
cfg := c.createConfiguration() | ||
|
||
c.APIClient = apiclient.NewAPIClient(cfg) | ||
_, ret, err := c.APIClient.AuthenticationApi.Cloudapi100SessionsPost(auth) | ||
if err != nil { | ||
return nil, fmt.Errorf("%w : %v", ErrAuthFailed, err) | ||
} | ||
token := ret.Header.Get("x-vmware-vcloud-access-token") | ||
if token == "" { | ||
return nil, ErrTokenEmpty | ||
} | ||
|
||
c.Auth = createTokenInContext(token) | ||
return c, nil | ||
} | ||
|
||
// createBasicAuthContext creates a new context with the basic auth values. | ||
func (c *CloudAvenue) createBasicAuthContext() context.Context { | ||
// Create a new CloudAvenue client using the configuration values | ||
auth := context.WithValue(context.Background(), apiclient.ContextBasicAuth, apiclient.BasicAuth{ | ||
UserName: c.User + "@" + c.Org, | ||
Password: c.Password, | ||
}) | ||
|
||
return auth | ||
} | ||
|
||
// createConfiguration creates a new configuration for the CloudAvenue client. | ||
func (c *CloudAvenue) createConfiguration() *apiclient.Configuration { | ||
cfg := &apiclient.Configuration{ | ||
BasePath: c.URL, | ||
DefaultHeader: make(map[string]string), | ||
UserAgent: fmt.Sprintf("Terraform/%s CloudAvenue/%s", c.TerraformVersion, c.CloudAvenueVersion), | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
// createTokenInContext creates a new context with the token value. | ||
func createTokenInContext(token string) context.Context { | ||
return context.WithValue(context.Background(), apiclient.ContextAccessToken, token) | ||
} |
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,74 @@ | ||
package client | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
apiclient "github.com/orange-cloudavenue/cloudavenue-sdk-go" | ||
) | ||
|
||
func TestCloudAvenueClient(t *testing.T) { | ||
t.Parallel() | ||
t.Run("CreateBasicAuthContext", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ca := CloudAvenue{ | ||
User: "dasilva", | ||
Password: "dasilva", | ||
Org: "acme", | ||
} | ||
|
||
authCtx := ca.createBasicAuthContext() | ||
auth, isBasicAuth := authCtx.Value(apiclient.ContextBasicAuth).(apiclient.BasicAuth) | ||
if !isBasicAuth { | ||
t.Fatalf("expected context with cloudavenue.BasicAuth value, got %v", reflect.TypeOf(authCtx.Value(apiclient.ContextBasicAuth))) | ||
} | ||
|
||
if auth.UserName != "dasilva@acme" { | ||
t.Fatalf("expected username to be %q, got %q", "dasilva@acme", auth.UserName) | ||
} | ||
|
||
if auth.Password != "dasilva" { | ||
t.Fatalf("expected password to be %q, got %q", "dasilva", auth.Password) | ||
} | ||
}) | ||
|
||
t.Run("CreateConfiguration", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ca := CloudAvenue{ | ||
TerraformVersion: "0.13.0", | ||
CloudAvenueVersion: "0.1.0", | ||
URL: "https://console1.cloudavenue.orange-business.com", | ||
} | ||
|
||
cfg := ca.createConfiguration() | ||
emptyMap := make(map[string]string) | ||
|
||
if cfg.BasePath != "https://console1.cloudavenue.orange-business.com" { | ||
t.Fatalf("expected base path to be %q, got %q", "https://console1.cloudavenue.orange-business.com", cfg.BasePath) | ||
} | ||
|
||
if cfg.UserAgent != "Terraform/0.13.0 CloudAvenue/0.1.0" { | ||
t.Fatalf("expected user agent to be %q, got %q", "Terraform/0.13.0 CloudAvenue/0.1.0", cfg.UserAgent) | ||
} | ||
|
||
if !reflect.DeepEqual(cfg.DefaultHeader, emptyMap) { | ||
t.Fatalf("expected default header to be %v, got %v", emptyMap, cfg.DefaultHeader) | ||
} | ||
}) | ||
|
||
t.Run("CreateTokenContext", func(t *testing.T) { | ||
t.Parallel() | ||
authCtx := createTokenInContext("t0k3n") | ||
token, isString := authCtx.Value(apiclient.ContextAccessToken).(string) | ||
|
||
if !isString { | ||
t.Fatalf("expected token with string value, got %v", reflect.TypeOf(authCtx.Value(apiclient.ContextAccessToken))) | ||
} | ||
|
||
if token != "t0k3n" { | ||
t.Fatalf("expected token to be %s, got %s", "t0k3n", token) | ||
} | ||
}) | ||
} |
2 changes: 1 addition & 1 deletion
2
internal/provider/helper_api_errors.go → internal/helpers/api_errors.go
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,4 +1,4 @@ | ||
package provider | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
|
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
2 changes: 1 addition & 1 deletion
2
internal/provider/helper_config.go → internal/helpers/config.go
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,70 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client" | ||
) | ||
|
||
type JobStatusMessage string | ||
|
||
const ( | ||
DONE JobStatusMessage = "DONE" | ||
FAILED JobStatusMessage = "FAILED" | ||
CREATED JobStatusMessage = "CREATED" | ||
PENDING JobStatusMessage = "PENDING" | ||
INPROGRESS JobStatusMessage = "IN_PROGRESS" | ||
ERROR JobStatusMessage = "ERROR" | ||
) | ||
|
||
// GetJobStatus is a helper function to get the status of a job. | ||
func GetJobStatus( | ||
ctx context.Context, | ||
client *client.CloudAvenue, | ||
jobID string, | ||
) (JobStatusMessage, error) { | ||
jobStatus, _, err := client.APIClient.JobsApi.ApiCustomersV10JobsJobIdGet(ctx, jobID) | ||
if err != nil { | ||
return "", err | ||
} | ||
return parseJobStatus(jobStatus[0].Status), nil | ||
} | ||
|
||
// parseJobStatus return the status of a job. | ||
func parseJobStatus(str string) JobStatusMessage { | ||
switch str { | ||
case "DONE": | ||
return DONE | ||
case "FAILED": | ||
return FAILED | ||
case "CREATED": | ||
return CREATED | ||
case "PENDING": | ||
return PENDING | ||
case "IN_PROGRESS": | ||
return INPROGRESS | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
// string is a stringer interface for jobStatus | ||
func (j JobStatusMessage) String() string { | ||
return strings.ToLower(string(j)) | ||
} | ||
|
||
// isDone is a helper function to check if a job is done. | ||
func (j JobStatusMessage) IsDone() bool { | ||
return j == DONE | ||
} | ||
|
||
// JobStatePending is a helper function to return an array of pending states. | ||
func JobStatePending() []string { | ||
return []string{CREATED.String(), INPROGRESS.String(), PENDING.String()} | ||
} | ||
|
||
// JobStateDone is a helper function to return an array of done states. | ||
func JobStateDone() []string { | ||
return []string{DONE.String()} | ||
} |
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
Oops, something went wrong.