Преглед изворни кода

Very basic working apply CLI command

Mohammed Nafees пре 4 година
родитељ
комит
be812aa37d
4 измењених фајлова са 148 додато и 27 уклоњено
  1. 140 1
      cli/cmd/apply.go
  2. 0 25
      cli/cmd/apply/porter_driver.go
  3. 1 1
      go.mod
  4. 7 0
      go.sum

+ 140 - 1
cli/cmd/apply.go

@@ -3,12 +3,17 @@ package cmd
 import (
 	"io/ioutil"
 	"os"
+	"strconv"
 
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/switchboard/pkg/drivers"
+	"github.com/porter-dev/switchboard/pkg/models"
 	"github.com/porter-dev/switchboard/pkg/parser"
 	switchboardTypes "github.com/porter-dev/switchboard/pkg/types"
 	"github.com/porter-dev/switchboard/pkg/worker"
+	"github.com/porter-dev/switchboard/utils/objutils"
+	"github.com/rs/zerolog"
 	"github.com/spf13/cobra"
 )
 
@@ -35,7 +40,7 @@ func init() {
 	applyCmd.MarkFlagRequired("file")
 }
 
-func apply(user *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+func apply(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
 	fileBytes, err := ioutil.ReadFile(porterYAML)
 	if err != nil {
 		return err
@@ -53,8 +58,142 @@ func apply(user *types.GetAuthenticatedUserResponse, client *api.Client, args []
 	}
 
 	worker := worker.NewWorker()
+	worker.RegisterDriver("porter.deploy", NewPorterDriver)
 
 	return worker.Apply(resGroup, &switchboardTypes.ApplyOpts{
 		BasePath: basePath,
 	})
 }
+
+type Source struct {
+	Name    string
+	Repo    string
+	Version string
+}
+
+type Target struct {
+	Project   uint
+	Cluster   uint
+	Namespace string
+}
+
+type Driver struct {
+	source      *Source
+	target      *Target
+	output      map[string]interface{}
+	lookupTable *map[string]drivers.Driver
+	logger      *zerolog.Logger
+}
+
+func NewPorterDriver(resource *models.Resource, opts *drivers.SharedDriverOpts) (drivers.Driver, error) {
+	driver := &Driver{
+		lookupTable: opts.DriverLookupTable,
+		logger:      opts.Logger,
+	}
+
+	if resource.Source != nil {
+		source, err := getSource(resource.Source)
+		if err != nil {
+			return nil, err
+		}
+		driver.source = source
+	} else {
+		// default source
+		driver.source = &Source{
+			Name:    "web",
+			Repo:    "https://charts.getporter.dev",
+			Version: "v0.13.0",
+		}
+	}
+
+	if resource.Target != nil {
+		target, err := getTarget(resource.Target)
+		if err != nil {
+			return nil, err
+		}
+		driver.target = target
+	} else {
+		// default target
+		driver.target = &Target{
+			Project:   config.Project,
+			Cluster:   config.Cluster,
+			Namespace: "default",
+		}
+	}
+
+	return driver, nil
+}
+
+func (d *Driver) ShouldApply(resource *models.Resource) bool {
+	return true
+}
+
+func (d *Driver) Apply(resource *models.Resource) (*models.Resource, error) {
+	app = resource.Name
+	err := updateFull(nil, GetAPIClient(config), []string{})
+	if err != nil {
+		return nil, err
+	}
+
+	return resource, nil
+}
+
+func (d *Driver) Output() (map[string]interface{}, error) {
+	return d.output, nil
+}
+
+func getSource(genericSource map[string]interface{}) (*Source, error) {
+	source := &Source{}
+
+	name, err := objutils.GetNestedString(genericSource, "name")
+	if err != nil {
+		return nil, err
+	}
+	source.Name = name
+
+	repo, err := objutils.GetNestedString(genericSource, "repo")
+	if err != nil {
+		return nil, err
+	}
+	source.Repo = repo
+
+	version, err := objutils.GetNestedString(genericSource, "version")
+	if err != nil {
+		return nil, err
+	}
+	source.Version = version
+
+	return source, nil
+}
+
+func getTarget(genericTarget map[string]interface{}) (*Target, error) {
+	target := &Target{}
+
+	project, err := objutils.GetNestedString(genericTarget, "project")
+	if err != nil {
+		return nil, err
+	}
+	projectNum, err := strconv.Atoi(project)
+	if err != nil {
+		return nil, err
+	}
+	target.Project = uint(projectNum)
+
+	cluster, err := objutils.GetNestedString(genericTarget, "cluster")
+	if err != nil {
+		return nil, err
+	}
+	clusterNum, err := strconv.Atoi(cluster)
+	if err != nil {
+		return nil, err
+	}
+	target.Cluster = uint(clusterNum)
+
+	namespace, err := objutils.GetNestedString(genericTarget, "namespace")
+	if err != nil {
+		return nil, err
+	}
+	target.Namespace = namespace
+
+	return target, nil
+}

+ 0 - 25
cli/cmd/apply/porter_driver.go

@@ -1,25 +0,0 @@
-package apply
-
-import (
-	"github.com/porter-dev/switchboard/internal/models"
-	"github.com/porter-dev/switchboard/pkg/drivers"
-)
-
-type Driver struct {
-}
-
-func NewPorterDriver() drivers.Driver {
-	return &Driver{}
-}
-
-func (d *Driver) ShouldApply(resource *models.Resource) bool {
-
-}
-
-func (d *Driver) Apply(resource *models.Resource) (*models.Resource, error) {
-
-}
-
-func (d *Driver) Output() (map[string]interface{}, error) {
-
-}

+ 1 - 1
go.mod

@@ -43,7 +43,7 @@ require (
 	github.com/opencontainers/image-spec v1.0.1
 	github.com/pelletier/go-toml v1.9.4 // indirect
 	github.com/pkg/errors v0.9.1
-	github.com/porter-dev/switchboard v0.0.0-20211202170709-ee9682ef7f4e
+	github.com/porter-dev/switchboard v0.0.0-20211203120508-691813a27c1b
 	github.com/rs/zerolog v1.26.0
 	github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 // indirect
 	github.com/sendgrid/rest v2.6.3+incompatible // indirect

+ 7 - 0
go.sum

@@ -463,6 +463,7 @@ github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d/go.mod h1:ZZM
 github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
 github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
 github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
+github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
 github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
 github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
 github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
@@ -975,6 +976,7 @@ github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlW
 github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
 github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
 github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
+github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
 github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
 github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
 github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
@@ -984,6 +986,7 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
 github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
 github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
 github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
+github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
 github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
 github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
 github.com/mattn/go-oci8 v0.1.1/go.mod h1:wjDx6Xm9q7dFtHJvIlrI99JytznLw5wQ4R+9mNXJwGI=
@@ -1171,6 +1174,10 @@ github.com/porter-dev/switchboard v0.0.0-20211201193634-43a3675cb104 h1:GopEu0kL
 github.com/porter-dev/switchboard v0.0.0-20211201193634-43a3675cb104/go.mod h1:qsttYk/Lj7CDWJpQ2vEF2Vxz9PEAe6BmpzrEPt36zMg=
 github.com/porter-dev/switchboard v0.0.0-20211202170709-ee9682ef7f4e h1:kq7csTJ573FwMoRQ/47g1JhS3lDoTq40sXi4tZzpL7Y=
 github.com/porter-dev/switchboard v0.0.0-20211202170709-ee9682ef7f4e/go.mod h1:qsttYk/Lj7CDWJpQ2vEF2Vxz9PEAe6BmpzrEPt36zMg=
+github.com/porter-dev/switchboard v0.0.0-20211203113052-82c227d9c378 h1:CBKhpruNzHjYIjkWKboCAEkAt62gcK4X2Yl9O0pQgEw=
+github.com/porter-dev/switchboard v0.0.0-20211203113052-82c227d9c378/go.mod h1:xSPzqSFMQ6OSbp42fhCi4AbGbQbsm6nRvOkrblFeXU4=
+github.com/porter-dev/switchboard v0.0.0-20211203120508-691813a27c1b h1:xcM7l5Bzhi4xQsMEGr5MELPWMesVBMQdMqD6AUh5wiw=
+github.com/porter-dev/switchboard v0.0.0-20211203120508-691813a27c1b/go.mod h1:xSPzqSFMQ6OSbp42fhCi4AbGbQbsm6nRvOkrblFeXU4=
 github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
 github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
 github.com/prometheus/client_golang v0.0.0-20180209125602-c332b6f63c06/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=