Skip to content

Commit 152d874

Browse files
Retrive and use cloud creds from client (#117)
1 parent b58e487 commit 152d874

File tree

21 files changed

+458
-48
lines changed

21 files changed

+458
-48
lines changed

api/deploy/deploy.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package deploy
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"github.com/hashicorp/hcl/v2"
89
"github.com/multycloud/multy/api/converter"
910
"github.com/multycloud/multy/api/errors"
1011
"github.com/multycloud/multy/api/proto/common"
1112
"github.com/multycloud/multy/api/proto/config"
13+
"github.com/multycloud/multy/api/proto/creds"
1214
"github.com/multycloud/multy/api/proto/resources"
15+
"github.com/multycloud/multy/api/util"
1316
"github.com/multycloud/multy/decoder"
1417
"github.com/multycloud/multy/encoder"
1518
common_resources "github.com/multycloud/multy/resources"
@@ -31,7 +34,7 @@ const (
3134
tfState = "terraform.tfstate"
3235
)
3336

34-
func Translate(c *config.Config, prev *config.Resource, curr *config.Resource) (string, error) {
37+
func Translate(credentials *creds.CloudCredentials, c *config.Config, prev *config.Resource, curr *config.Resource) (string, error) {
3538
// TODO: get rid of this translation layer and instead use protos directly
3639
translated := map[string]common_resources.CloudSpecificResource{}
3740
for _, r := range c.Resources {
@@ -147,7 +150,16 @@ func Translate(c *config.Config, prev *config.Resource, curr *config.Resource) (
147150
Providers: provider,
148151
}
149152

150-
hclOutput, errs, err := encoder.Encode(&decodedResources)
153+
for _, r := range translated {
154+
if string(r.Cloud) == "aws" && credentials.AwsCreds == nil {
155+
return "", fmt.Errorf("aws credentials are required but not set")
156+
}
157+
if string(r.Cloud) == "azure" && credentials.AzureCreds == nil {
158+
return "", fmt.Errorf("azure credentials are required but not set")
159+
}
160+
}
161+
162+
hclOutput, errs, err := encoder.Encode(&decodedResources, credentials)
151163
if len(errs) > 0 {
152164
return hclOutput, errors.ValidationErrors(errs)
153165
}
@@ -158,9 +170,13 @@ func Translate(c *config.Config, prev *config.Resource, curr *config.Resource) (
158170
return hclOutput, nil
159171
}
160172

161-
func Deploy(c *config.Config, prev *config.Resource, curr *config.Resource) (*output.TfState, error) {
173+
func Deploy(ctx context.Context, c *config.Config, prev *config.Resource, curr *config.Resource) (*output.TfState, error) {
162174

163-
hclOutput, err := Translate(c, prev, curr)
175+
credentials, err := util.ExtractCloudCredentials(ctx)
176+
if err != nil {
177+
return nil, err
178+
}
179+
hclOutput, err := Translate(credentials, c, prev, curr)
164180
if err != nil {
165181
return nil, err
166182
}

api/errors/errors.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ package errors
33
import (
44
"fmt"
55
pberr "github.com/multycloud/multy/api/proto/errors"
6-
"github.com/multycloud/multy/util"
76
"github.com/multycloud/multy/validate"
87
"google.golang.org/genproto/googleapis/rpc/code"
98
spb "google.golang.org/genproto/googleapis/rpc/status"
109
"google.golang.org/grpc/codes"
1110
"google.golang.org/grpc/status"
12-
"google.golang.org/protobuf/types/known/anypb"
1311
)
1412

1513
func PermissionDenied(msg string) error {
@@ -35,18 +33,15 @@ func InternalServerErrorWithMessage(msg string, err error) error {
3533
}
3634

3735
func ValidationErrors(errs []validate.ValidationError) error {
38-
return status.ErrorProto(&spb.Status{
39-
Code: int32(code.Code_INVALID_ARGUMENT),
40-
Message: fmt.Sprintf("%d validation errors found", len(errs)),
41-
Details: util.MapSliceValues(errs, func(e validate.ValidationError) *anypb.Any {
42-
a, _ := anypb.New(&pberr.ResourceValidationError{
43-
ResourceId: e.ResourceId,
44-
ErrorMessage: e.ErrorMessage,
45-
FieldName: e.FieldName,
46-
})
47-
return a
48-
}),
49-
})
36+
st := status.New(codes.InvalidArgument, fmt.Sprintf("%d validation errors found", len(errs)))
37+
for _, e := range errs {
38+
st, _ = st.WithDetails(&pberr.ResourceValidationError{
39+
ResourceId: e.ResourceId,
40+
ErrorMessage: e.ErrorMessage,
41+
FieldName: e.FieldName,
42+
})
43+
}
44+
return st.Err()
5045
}
5146

5247
func ResourceNotFound(resourceId string) error {

0 commit comments

Comments
 (0)