Skip to content

Commit

Permalink
Serialize all of massdriver.yaml and add to 'spec' field in publish b…
Browse files Browse the repository at this point in the history
…ody (#142)

* Serialize all of massdriver.yaml and add to 'spec' field in publish body

* update another warning message to use orange

* adjust defaulting of steps to be a part of import
  • Loading branch information
chrisghill authored Oct 4, 2024
1 parent 06e11ca commit 1c441e6
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 24 deletions.
32 changes: 26 additions & 6 deletions pkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"

"github.com/massdriver-cloud/mass/pkg/files"
"github.com/massdriver-cloud/mass/pkg/prettylogs"
"github.com/massdriver-cloud/mass/pkg/restclient"
)

Expand Down Expand Up @@ -71,22 +72,29 @@ func (b *Bundle) GenerateBundlePublishBody(srcDir string) (restclient.PublishPos
body.UISchema = b.UI

var appSpec map[string]interface{}

marshalledAppSpec, err := json.Marshal(b.AppSpec)

if err != nil {
return restclient.PublishPost{}, err
}

err = json.Unmarshal(marshalledAppSpec, &appSpec)

if err != nil {
fmt.Println(err)
return restclient.PublishPost{}, err
}

body.AppSpec = appSpec

var bundleSpec map[string]interface{}
marshalledBundleSpec, err := json.Marshal(b)
if err != nil {
return restclient.PublishPost{}, err
}
err = json.Unmarshal(marshalledBundleSpec, &bundleSpec)
if err != nil {
fmt.Println(err)
return restclient.PublishPost{}, err
}
body.Spec = bundleSpec

err = checkForOperatorGuideAndSetValue(srcDir, &body)

if err != nil {
Expand All @@ -97,7 +105,6 @@ func (b *Bundle) GenerateBundlePublishBody(srcDir string) (restclient.PublishPos
}

func (b *Bundle) IsInfrastructure() bool {
// a Deprecation warning is printed in the bundle parse function
return b.Type == "bundle" || b.Type == "infrastructure"
}

Expand Down Expand Up @@ -146,6 +153,8 @@ func UnmarshalAndApplyDefaults(readDirectory string) (*Bundle, error) {
ApplyAppBlockDefaults(unmarshalledBundle)
}

applyStepDefaults(unmarshalledBundle)

// This looks weird but we have to be careful we don't overwrite things that do exist in the bundle file
if unmarshalledBundle.Connections == nil {
unmarshalledBundle.Connections = make(map[string]any)
Expand Down Expand Up @@ -180,6 +189,17 @@ func ApplyAppBlockDefaults(b *Bundle) {
}
}

func applyStepDefaults(b *Bundle) {
if b.Steps == nil || len(b.Steps) == 0 {
msg := fmt.Sprintf(`%s: No steps defined in massdriver.yaml, defaulting to Terraform provisioner. This will be deprecated in a future release. To avoid this warning, please add the following to massdriver.yaml:
steps:
path: src
provisioner: terraform`, prettylogs.Orange("Warning"))
fmt.Println(msg + "\n")
b.Steps = append(b.Steps, Step{Path: "src", Provisioner: "terraform"})
}
}

func parseMetadataSchema() map[string]interface{} {
metadataBytes, err := embedFS.ReadFile("schemas/metadata-schema.json")
if err != nil {
Expand Down
12 changes: 1 addition & 11 deletions pkg/commands/build_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func BuildBundle(buildPath string, b *bundle.Bundle, c *restclient.MassdriverCli
}

combined := b.CombineParamsConnsMetadata()
for _, step := range stepsOrDefault(b.Steps) {
for _, step := range b.Steps {
prov := provisioners.NewProvisioner(step.Provisioner)
err = prov.ExportMassdriverInputs(path.Join(buildPath, step.Path), combined)
if err != nil {
Expand All @@ -30,13 +30,3 @@ func BuildBundle(buildPath string, b *bundle.Bundle, c *restclient.MassdriverCli

return nil
}

func stepsOrDefault(steps []bundle.Step) []bundle.Step {
if steps == nil {
return []bundle.Step{
{Path: "src", Provisioner: "terraform"},
}
}

return steps
}
2 changes: 1 addition & 1 deletion pkg/commands/import_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func ImportParams(buildPath string, skipVerify bool) error {
}

missing := map[string]any{}
for _, step := range stepsOrDefault(b.Steps) {
for _, step := range b.Steps {
prov := provisioners.NewProvisioner(step.Provisioner)
inputs, readProvErr := prov.ReadProvisionerInputs(path.Join(buildPath, step.Path))
if readProvErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/publish/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestPublishBundle(t *testing.T) {
t.Fatal(err)
}

wantPublishBody := `{"name":"the-bundle","description":"something","type":"bundle","source_url":"github.com/some-repo","access":"public","artifacts_schema":{"artifacts":"foo"},"connections_schema":{"connections":"bar"},"params_schema":{"params":{"hello":"world"}},"ui_schema":{"ui":"baz"},"operator_guide":"IyBTb21lIE1hcmtkb3duIQo=","app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}}}`
wantPublishBody := `{"name":"the-bundle","description":"something","type":"bundle","source_url":"github.com/some-repo","access":"public","artifacts_schema":{"artifacts":"foo"},"connections_schema":{"connections":"bar"},"params_schema":{"params":{"hello":"world"}},"ui_schema":{"ui":"baz"},"operator_guide":"IyBTb21lIE1hcmtkb3duIQo=","app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}},"spec":{"access":"public","app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}},"artifacts":{"artifacts":"foo"},"connections":{"connections":"bar"},"description":"something","name":"the-bundle","params":{"params":{"hello":"world"}},"schema":"","source_url":"github.com/some-repo","steps":[{"path":"deploy","provisioner":"opentofu"}],"type":"bundle","ui":{"ui":"baz"}}}`

if string(gotPublishBody) != wantPublishBody {
t.Errorf("expected publish body to be %s but got %s", wantPublishBody, gotPublishBody)
Expand Down
32 changes: 28 additions & 4 deletions pkg/commands/publish/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func TestPublish(t *testing.T) {
SourceURL: "github.com/some-repo",
Type: "bundle",
Access: "public",
Steps: []bundle.Step{
{
Path: "deploy",
Provisioner: "opentofu",
},
},
Artifacts: map[string]interface{}{
"artifacts": "foo",
},
Expand All @@ -53,7 +59,7 @@ func TestPublish(t *testing.T) {
},
AppSpec: nil,
},
wantBody: `{"name":"the-bundle","description":"something","type":"bundle","source_url":"github.com/some-repo","access":"public","artifacts_schema":{"artifacts":"foo"},"connections_schema":{"connections":"bar"},"params_schema":{"params":{"hello":"world"}},"ui_schema":{"ui":"baz"}}`,
wantBody: `{"name":"the-bundle","description":"something","type":"bundle","source_url":"github.com/some-repo","access":"public","artifacts_schema":{"artifacts":"foo"},"connections_schema":{"connections":"bar"},"params_schema":{"params":{"hello":"world"}},"ui_schema":{"ui":"baz"},"spec":{"access":"public","artifacts":{"artifacts":"foo"},"connections":{"connections":"bar"},"description":"something","name":"the-bundle","params":{"params":{"hello":"world"}},"schema":"","source_url":"github.com/some-repo","steps":[{"path":"deploy","provisioner":"opentofu"}],"type":"bundle","ui":{"ui":"baz"}}}`,
},
{
name: "Submits an app block field if one does exist",
Expand All @@ -64,6 +70,12 @@ func TestPublish(t *testing.T) {
SourceURL: "github.com/some-repo",
Type: "bundle",
Access: "public",
Steps: []bundle.Step{
{
Path: "deploy",
Provisioner: "opentofu",
},
},
Artifacts: map[string]interface{}{
"artifacts": "foo",
},
Expand Down Expand Up @@ -93,7 +105,7 @@ func TestPublish(t *testing.T) {
},
},
},
wantBody: `{"name":"the-bundle","description":"something","type":"bundle","source_url":"github.com/some-repo","access":"public","artifacts_schema":{"artifacts":"foo"},"connections_schema":{"connections":"bar"},"params_schema":{"params":{"hello":"world"}},"ui_schema":{"ui":"baz"},"app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}}}`,
wantBody: `{"name":"the-bundle","description":"something","type":"bundle","source_url":"github.com/some-repo","access":"public","artifacts_schema":{"artifacts":"foo"},"connections_schema":{"connections":"bar"},"params_schema":{"params":{"hello":"world"}},"ui_schema":{"ui":"baz"},"app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}},"spec":{"access":"public","app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}},"artifacts":{"artifacts":"foo"},"connections":{"connections":"bar"},"description":"something","name":"the-bundle","params":{"params":{"hello":"world"}},"schema":"","source_url":"github.com/some-repo","steps":[{"path":"deploy","provisioner":"opentofu"}],"type":"bundle","ui":{"ui":"baz"}}}`,
},
{
name: "Submits an operator.md guide if it exist",
Expand All @@ -104,6 +116,12 @@ func TestPublish(t *testing.T) {
SourceURL: "github.com/some-repo",
Type: "bundle",
Access: "public",
Steps: []bundle.Step{
{
Path: "deploy",
Provisioner: "opentofu",
},
},
Artifacts: map[string]interface{}{
"artifacts": "foo",
},
Expand Down Expand Up @@ -133,7 +151,7 @@ func TestPublish(t *testing.T) {
},
},
},
wantBody: `{"name":"the-bundle","description":"something","type":"bundle","source_url":"github.com/some-repo","access":"public","artifacts_schema":{"artifacts":"foo"},"connections_schema":{"connections":"bar"},"params_schema":{"params":{"hello":"world"}},"ui_schema":{"ui":"baz"},"operator_guide":"IyBTb21lIE1hcmtkb3duIQo=","app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}}}`,
wantBody: `{"name":"the-bundle","description":"something","type":"bundle","source_url":"github.com/some-repo","access":"public","artifacts_schema":{"artifacts":"foo"},"connections_schema":{"connections":"bar"},"params_schema":{"params":{"hello":"world"}},"ui_schema":{"ui":"baz"},"operator_guide":"IyBTb21lIE1hcmtkb3duIQo=","app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}},"spec":{"access":"public","app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}},"artifacts":{"artifacts":"foo"},"connections":{"connections":"bar"},"description":"something","name":"the-bundle","params":{"params":{"hello":"world"}},"schema":"","source_url":"github.com/some-repo","steps":[{"path":"deploy","provisioner":"opentofu"}],"type":"bundle","ui":{"ui":"baz"}}}`,
},
{
name: "Submits an operator.mdx guide if it exist",
Expand All @@ -144,6 +162,12 @@ func TestPublish(t *testing.T) {
SourceURL: "github.com/some-repo",
Type: "bundle",
Access: "public",
Steps: []bundle.Step{
{
Path: "deploy",
Provisioner: "opentofu",
},
},
Artifacts: map[string]interface{}{
"artifacts": "foo",
},
Expand Down Expand Up @@ -173,7 +197,7 @@ func TestPublish(t *testing.T) {
},
},
},
wantBody: `{"name":"the-bundle","description":"something","type":"bundle","source_url":"github.com/some-repo","access":"public","artifacts_schema":{"artifacts":"foo"},"connections_schema":{"connections":"bar"},"params_schema":{"params":{"hello":"world"}},"ui_schema":{"ui":"baz"},"operator_guide":"IyBTb21lIE1hcmtkb3duIQo=","app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}}}`,
wantBody: `{"name":"the-bundle","description":"something","type":"bundle","source_url":"github.com/some-repo","access":"public","artifacts_schema":{"artifacts":"foo"},"connections_schema":{"connections":"bar"},"params_schema":{"params":{"hello":"world"}},"ui_schema":{"ui":"baz"},"operator_guide":"IyBTb21lIE1hcmtkb3duIQo=","app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}},"spec":{"access":"public","app":{"envs":{"LOG_LEVEL":"warn"},"policies":[".connections.vpc.data.infrastructure.arn"],"secrets":{"STRIPE_KEY":{"description":"Access key for live stripe accounts","json":false,"required":true,"title":"A secret"}}},"artifacts":{"artifacts":"foo"},"connections":{"connections":"bar"},"description":"something","name":"the-bundle","params":{"params":{"hello":"world"}},"schema":"","source_url":"github.com/some-repo","steps":[{"path":"deploy","provisioner":"opentofu"}],"type":"bundle","ui":{"ui":"baz"}}}`,
},
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"

"github.com/massdriver-cloud/mass/pkg/api"
"github.com/massdriver-cloud/mass/pkg/prettylogs"
"github.com/sethvargo/go-envconfig"
)

Expand Down Expand Up @@ -37,7 +38,7 @@ func Get() (*Config, error) {
}
uuidRegex := `^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$`
if regexp.MustCompile(uuidRegex).MatchString(c.OrgID) {
fmt.Println("WARNING: environment variable MASSDRIVER_ORG_ID is a UUID. This is deprecated and will be removed in a future release. Please use the organization abbreviation instead.")
fmt.Printf("%s: environment variable MASSDRIVER_ORG_ID is a UUID. This is deprecated and will be removed in a future release. Please use the organization abbreviation instead.\n", prettylogs.Orange("Warning"))
}

setDefaults(&c)
Expand Down
4 changes: 4 additions & 0 deletions pkg/prettylogs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ func Underline(word string) lipgloss.Style {
func Green(word string) lipgloss.Style {
return lipgloss.NewStyle().SetString(word).Foreground(lipgloss.Color("#00FF00"))
}

func Orange(word string) lipgloss.Style {
return lipgloss.NewStyle().SetString(word).Foreground(lipgloss.Color("#FFA500"))
}
1 change: 1 addition & 0 deletions pkg/restclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type PublishPost struct {
UISchema map[string]interface{} `json:"ui_schema"`
OperatorGuide []byte `json:"operator_guide,omitempty"`
AppSpec map[string]interface{} `json:"app,omitempty"`
Spec map[string]interface{} `json:"spec,omitempty"`
}

type PublishResponse struct {
Expand Down

0 comments on commit 1c441e6

Please sign in to comment.