Skip to content

Commit

Permalink
Operation (#177)
Browse files Browse the repository at this point in the history
* added list and describe operation

* added list, describe operation and operate component commands

* added list, describe operation and operate component commands

* linting fixes

* blocked service redploy

* removed redeploy block from cli

---------

Co-authored-by: rahulrayal <[email protected]>
  • Loading branch information
yogesh-badke and rahulrayal authored Mar 9, 2023
1 parent 6ca98fd commit 5407de7
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 70 deletions.
60 changes: 9 additions & 51 deletions api/component/component.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,16 @@
package component

// Component interface
type Component struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Type string `yaml:"type,omitempty" json:"type,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
Config interface{} `yaml:"config,omitempty" json:"config,omitempty"`
Deployment interface{} `yaml:"deployment_config,omitempty" json:"deployment_config,omitempty"`
Scaling interface{} `yaml:"scaling_config,omitempty" json:"scaling_config,omitempty"`
Discovery interface{} `yaml:"discovery_config,omitempty" json:"discovery_config,omitempty"`
DeploymentPlatformMapping interface{} `yaml:"behaviour,omitempty" json:"behaviour,omitempty"`
type Operation struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Values interface{} `yaml:"values,omitempty" json:"values,omitempty"`
}

// Type interface
type Type struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
TotalVersions int `yaml:"totalVersions,omitempty" json:"totalVersions,omitempty"`
DeploymentTypes []string `yaml:"deployment_types,omitempty" json:"deployment_types,omitempty"`
CreatedBy string `yaml:"createdBy,omitempty" json:"createdBy,omitempty"`
UpdatedBy string `yaml:"updatedBy,omitempty" json:"updatedBy,omitempty"`
CreatedAt string `yaml:"createdAt,omitempty" json:"createdAt,omitempty"`
UpdatedAt string `yaml:"updatedAt,omitempty" json:"updatedAt,omitempty"`
Config interface{} `yaml:"config,omitempty" json:"config,omitempty"`
Deployment interface{} `yaml:"deployment_config,omitempty" json:"deployment_config,omitempty"`
Scaling interface{} `yaml:"scaling_config,omitempty" json:"scaling_config,omitempty"`
Discovery interface{} `yaml:"discovery_config,omitempty" json:"discovery_config,omitempty"`
DeploymentPlatformMapping interface{} `yaml:"behaviour,omitempty" json:"behaviour,omitempty"`
type Data struct {
EnvName string `yaml:"env_name,omitempty" json:"env_name,omitempty"`
ServiceName string `yaml:"service_name,omitempty" json:"service_name,omitempty"`
Operations []Operation `yaml:"operations,omitempty" json:"operations,omitempty"`
}

// Exposed Config interface
type ExposedConfig struct {
Config string `yaml:"config,omitempty" json:"config,omitempty"`
Mandatory bool `yaml:"mandatory,omitempty" json:"mandatory,omitempty"`
DataType string `yaml:"data_type,omitempty" json:"data_type,omitempty"`
}

// ListTypeResponse interface
type ListTypeResponse struct {
Response []Type `yaml:"resp,omitempty" json:"resp,omitempty"`
}

// DetailComponentTypeResponse interface
type ComponentDetailsResponse struct {
Response ComponentDetails `yaml:"resp,omitempty" json:"resp,omitempty"`
}

// ComponentDetails interface
type ComponentDetails struct {
Details Type `yaml:"details,omitempty" json:"details,omitempty"`
ExposedConfigs []ExposedConfig `yaml:"exposed_config,omitempty" json:"exposed_config,omitempty"`
}

// ListTypeResponse interface
type DetailComponentResponse struct {
Response Component `yaml:"resp,omitempty" json:"resp,omitempty"`
type OperateComponentRequest struct {
Data Data `yaml:"data,omitempty" json:"data,omitempty"`
}
58 changes: 58 additions & 0 deletions api/componenttype/componenttype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package componenttype

// Component interface
type Component struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Type string `yaml:"type,omitempty" json:"type,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
Config interface{} `yaml:"config,omitempty" json:"config,omitempty"`
Deployment interface{} `yaml:"deployment_config,omitempty" json:"deployment_config,omitempty"`
Scaling interface{} `yaml:"scaling_config,omitempty" json:"scaling_config,omitempty"`
Discovery interface{} `yaml:"discovery_config,omitempty" json:"discovery_config,omitempty"`
DeploymentPlatformMapping interface{} `yaml:"behaviour,omitempty" json:"behaviour,omitempty"`
}

// Type interface
type Type struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
TotalVersions int `yaml:"totalVersions,omitempty" json:"totalVersions,omitempty"`
DeploymentTypes []string `yaml:"deployment_types,omitempty" json:"deployment_types,omitempty"`
CreatedBy string `yaml:"createdBy,omitempty" json:"createdBy,omitempty"`
UpdatedBy string `yaml:"updatedBy,omitempty" json:"updatedBy,omitempty"`
CreatedAt string `yaml:"createdAt,omitempty" json:"createdAt,omitempty"`
UpdatedAt string `yaml:"updatedAt,omitempty" json:"updatedAt,omitempty"`
Config interface{} `yaml:"config,omitempty" json:"config,omitempty"`
Deployment interface{} `yaml:"deployment_config,omitempty" json:"deployment_config,omitempty"`
Scaling interface{} `yaml:"scaling_config,omitempty" json:"scaling_config,omitempty"`
Discovery interface{} `yaml:"discovery_config,omitempty" json:"discovery_config,omitempty"`
DeploymentPlatformMapping interface{} `yaml:"behaviour,omitempty" json:"behaviour,omitempty"`
}

// Exposed Config interface
type ExposedConfig struct {
Config string `yaml:"config,omitempty" json:"config,omitempty"`
Mandatory bool `yaml:"mandatory,omitempty" json:"mandatory,omitempty"`
DataType string `yaml:"data_type,omitempty" json:"data_type,omitempty"`
}

// ListTypeResponse interface
type ListTypeResponse struct {
Response []Type `yaml:"resp,omitempty" json:"resp,omitempty"`
}

// DetailComponentTypeResponse interface
type ComponentDetailsResponse struct {
Response ComponentDetails `yaml:"resp,omitempty" json:"resp,omitempty"`
}

// ComponentDetails interface
type ComponentDetails struct {
Details Type `yaml:"details,omitempty" json:"details,omitempty"`
ExposedConfigs []ExposedConfig `yaml:"exposed_config,omitempty" json:"exposed_config,omitempty"`
}

// ListTypeResponse interface
type DetailComponentResponse struct {
Response Component `yaml:"resp,omitempty" json:"resp,omitempty"`
}
11 changes: 11 additions & 0 deletions api/operation/operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package operation

type Operation struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Description string `yaml:"description,omitempty" json:"description,omitempty"`
InputSchema interface{} `yaml:"inputSchema,omitempty" json:"inputSchema,omitempty"`
}

type ListOperation struct {
Response []Operation `yaml:"resp,omitempty" json:"resp,omitempty"`
}
26 changes: 13 additions & 13 deletions api/service/service.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package service

import (
"github.com/dream11/odin/api/component"
"github.com/dream11/odin/api/componenttype"
"github.com/dream11/odin/api/label"
)

type Service struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
Team string `yaml:"team,omitempty" json:"team,omitempty"`
Description string `yaml:"description,omitempty" json:"description,omitempty"`
CreatedBy string `yaml:"createdBy,omitempty" json:"createdBy,omitempty"`
UpdatedBy string `yaml:"updatedBy,omitempty" json:"updatedBy,omitempty"`
CreatedAt string `yaml:"createdAt,omitempty" json:"createdAt,omitempty"`
UpdatedAt string `yaml:"updatedAt,omitempty" json:"updatedAt,omitempty"`
Active *bool `yaml:"isActive,omitempty" json:"isActive,omitempty"`
Tags interface{} `yaml:"tags,omitempty" json:"tags,omitempty"`
Labels []label.Label `yaml:"labels,omitempty" json:"labels,omitempty"`
Components []component.Component `yaml:"components,omitempty" json:"components,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
Team string `yaml:"team,omitempty" json:"team,omitempty"`
Description string `yaml:"description,omitempty" json:"description,omitempty"`
CreatedBy string `yaml:"createdBy,omitempty" json:"createdBy,omitempty"`
UpdatedBy string `yaml:"updatedBy,omitempty" json:"updatedBy,omitempty"`
CreatedAt string `yaml:"createdAt,omitempty" json:"createdAt,omitempty"`
UpdatedAt string `yaml:"updatedAt,omitempty" json:"updatedAt,omitempty"`
Active *bool `yaml:"isActive,omitempty" json:"isActive,omitempty"`
Tags interface{} `yaml:"tags,omitempty" json:"tags,omitempty"`
Labels []label.Label `yaml:"labels,omitempty" json:"labels,omitempty"`
Components []componenttype.Component `yaml:"components,omitempty" json:"components,omitempty"`
}

// ListResponse interface
Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ type application struct {
// App (Application) interface
var App application = application{
Name: "odin",
Version: "1.2.4",
Version: "1.3.0-beta",
}
16 changes: 16 additions & 0 deletions internal/backend/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package backend

import (
"path"

"github.com/dream11/odin/api/component"
)

type Component struct{}

func (c *Component) OperateComponent(componentName string, data component.OperateComponentRequest) {
client := newStreamingApiClient()
client.Headers["Command-Verb"] = "operate"
response := client.streamWithRetry(path.Join("component", componentName, "operate"), "PUT", data)
response.Process(true)
}
10 changes: 5 additions & 5 deletions internal/backend/componentType.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ import (
"encoding/json"
"path"

"github.com/dream11/odin/api/component"
"github.com/dream11/odin/api/componenttype"
)

// ComponentType entity
type ComponentType struct{}

// ListComponentTypes : list all available component types
func (c *ComponentType) ListComponentTypes(componentTypeName, version string) ([]component.Type, error) {
func (c *ComponentType) ListComponentTypes(componentTypeName, version string) ([]componenttype.Type, error) {
client := newApiClient()
client.QueryParams["version"] = version
client.QueryParams["name"] = componentTypeName
response := client.actionWithRetry("componenttypes", "GET", nil)

response.Process(true) // process response and exit if error

var componentTypeResponse component.ListTypeResponse
var componentTypeResponse componenttype.ListTypeResponse
err := json.Unmarshal(response.Body, &componentTypeResponse)

return componentTypeResponse.Response, err
}

// DescribeComponentTypes : describe a component type
func (c *ComponentType) DescribeComponentType(componentTypeName, version string) (component.ComponentDetails, error) {
func (c *ComponentType) DescribeComponentType(componentTypeName, version string) (componenttype.ComponentDetails, error) {
client := newApiClient()
client.QueryParams["version"] = version
response := client.actionWithRetry(path.Join("componenttypes", componentTypeName), "GET", nil)
response.Process(true) // process response and exit if error

var componentDetailsResponse component.ComponentDetailsResponse
var componentDetailsResponse componenttype.ComponentDetailsResponse
err := json.Unmarshal(response.Body, &componentDetailsResponse)

return componentDetailsResponse.Response, err
Expand Down
19 changes: 19 additions & 0 deletions internal/backend/operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package backend

import (
"encoding/json"
"path"

operationapi "github.com/dream11/odin/api/operation"
)

type Operation struct{}

func (o *Operation) ListOperations(componentTypeName string) ([]operationapi.Operation, error) {
client := newApiClient()
response := client.actionWithRetry(path.Join("component", componentTypeName, "operate"), "GET", nil)
response.Process(true) // process response and exit if error
var listResponse operationapi.ListOperation
err := json.Unmarshal(response.Body, &listResponse)
return listResponse.Response, err
}
9 changes: 9 additions & 0 deletions internal/command/command_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ func CommandsCatalog() map[string]cli.CommandFactory {
"undeploy service-set": func() (cli.Command, error) {
return &commands.ServiceSet{Undeploy: true}, nil
},
"list operation": func() (cli.Command, error) {
return &commands.Operation{List: true}, nil
},
"describe operation": func() (cli.Command, error) {
return &commands.Operation{Describe: true}, nil
},
"operate component": func() (cli.Command, error) {
return &commands.Component{Operate: true}, nil
},

// Verb for application-template
"generate application-template": func() (cli.Command, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/command/commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type command struct {
Release bool // Release a resource record
Set bool // Set a default env
Update bool // Update a env
Operate bool // Operate on a component

Logger ui.Logger // Use this to log messages
Input ui.Input // Use this to take inputs
Expand Down
90 changes: 90 additions & 0 deletions internal/command/commands/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package commands

import (
"encoding/json"
"flag"
"fmt"

"github.com/dream11/odin/api/component"
"github.com/dream11/odin/internal/backend"
"github.com/dream11/odin/pkg/utils"
)

var componentClient backend.Component

type Component command

func (c *Component) Run(args []string) int {
// Define flag set
flagSet := flag.NewFlagSet("flagSet", flag.ContinueOnError)
name := flagSet.String("name", "", "name of the component")
serviceName := flagSet.String("service", "", "name of the service in which the component is deployed")
envName := flagSet.String("env", "", "name of the environment in which the service is deployed")
operation := flagSet.String("operation", "", "name of the operation to performed on the component")
options := flagSet.String("options", "", "options of the operation in JSON format")

err := flagSet.Parse(args)
if err != nil {
c.Logger.Error("Unable to parse flags! " + err.Error())
return 1
}

if c.Operate {
if *envName == "" {
*envName = utils.FetchKey(ENV_NAME_KEY)
}
emptyParameters := emptyParameters(map[string]string{"--name": *name, "--service": *serviceName, "--env": *envName, "--operation": *operation, "--options": *options})
if len(emptyParameters) == 0 {
var optionsJson interface{}
err = json.Unmarshal([]byte(*options), &optionsJson)
if err != nil {
c.Logger.Error("Unable to parse options JSON " + err.Error())
return 1
}

data := component.OperateComponentRequest{
Data: component.Data{
EnvName: *envName,
ServiceName: *serviceName,
Operations: []component.Operation{
{
Name: *operation,
Values: optionsJson,
},
},
},
}

componentClient.OperateComponent(*name, data)
return 0
}

c.Logger.Error(fmt.Sprintf("%s cannot be blank", emptyParameters))
return 1
}

c.Logger.Error("Not a valid command")
return 127
}

// Help : returns an explanatory string
func (c *Component) Help() string {
if c.Operate {
return commandHelper("operate", "component", "", []Options{
{Flag: "--name", Description: "name of the component"},
{Flag: "--service", Description: "name of the service in which the component is deployed"},
{Flag: "--env", Description: "name of the environment in which the service is deployed"},
{Flag: "--operation", Description: "name of the operation to performed on the component"},
{Flag: "--options", Description: "options of the operation in JSON format"},
})
}
return defaultHelper()
}

// Synopsis : returns a brief helper text for the command's verbs
func (c *Component) Synopsis() string {
if c.Operate {
return "Operate on a component"
}
return defaultHelper()
}
Loading

0 comments on commit 5407de7

Please sign in to comment.