Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions internal/handlers/descriptors/integrations.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
---
##############################################################################
# Reader Commands
##############################################################################

get:
use: integrations
group: admin-essentials
Expand All @@ -25,10 +21,6 @@ describe:
specified integration. If the integration does not exist, this command
will return an error.

##############################################################################
# Writer Commands
##############################################################################

create:
use: integration <name> --model <string>
group: admin-essentials
Expand Down Expand Up @@ -77,3 +69,15 @@ clear:
example: |
# Delete all configured integrations
$ ipctl clear integrations

import:
use: integration <path>
group: admin-essentials
description: |
Import an integration

export:
use: integration <name>
group: admin-essentials
description: |
Export an integration
94 changes: 92 additions & 2 deletions internal/runners/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/itential/ipctl/internal/flags"
"github.com/itential/ipctl/internal/terminal"
"github.com/itential/ipctl/internal/utils"
"github.com/itential/ipctl/pkg/client"
"github.com/itential/ipctl/pkg/config"
Expand All @@ -19,16 +20,24 @@ import (

type IntegrationRunner struct {
config *config.Config
client client.Client
service *services.IntegrationService
}

func NewIntegrationRunner(c client.Client, cfg *config.Config) *IntegrationRunner {
return &IntegrationRunner{
service: services.NewIntegrationService(c),
client: c,
config: cfg,
}
}

/*
******************************************************************************
Reader interface
******************************************************************************
*/

// Get implements the `get integration-models` command
func (r *IntegrationRunner) Get(in Request) (*Response, error) {
logger.Trace()
Expand Down Expand Up @@ -69,14 +78,19 @@ func (r *IntegrationRunner) Describe(in Request) (*Response, error) {
), nil
}

/*
******************************************************************************
Writer interface
******************************************************************************
*/

// Create implements the `create integration <name>` command
func (r *IntegrationRunner) Create(in Request) (*Response, error) {
logger.Trace()

name := in.Args[0]

var options flags.IntegrationCreateOptions
utils.LoadObject(in.Options, &options)
options := in.Options.(*flags.IntegrationCreateOptions)

if options.Replace {
existing, err := r.service.Get(name)
Expand Down Expand Up @@ -138,6 +152,12 @@ func (r *IntegrationRunner) Clear(in Request) (*Response, error) {
), nil
}

/*
******************************************************************************
Copier interface
******************************************************************************
*/

// Copy implements the `copy integration <name>` command
func (r *IntegrationRunner) Copy(in Request) (*Response, error) {
logger.Trace()
Expand Down Expand Up @@ -187,3 +207,73 @@ func (r *IntegrationRunner) Copy(in Request) (*Response, error) {
fmt.Sprintf("Successfully copied integration `%s` from `%s` to `%s`", name, common.From, common.To),
), nil
}

/*
*******************************************************************************
Exporter interface
*******************************************************************************
*/

// Export implements the `export integration ...` command
func (r *IntegrationRunner) Export(in Request) (*Response, error) {
logger.Trace()

name := in.Args[0]

res, err := r.service.Get(name)
if err != nil {
return nil, err
}

fn := fmt.Sprintf("%s.integration.json", name)

if err := exportAssetFromRequest(in, res, fn); err != nil {
return nil, err
}

return NewResponse(
fmt.Sprintf("Successfully exported integration `%s`", res.Name),
WithObject(res),
), nil
}

/*
*******************************************************************************
Importer interface
*******************************************************************************
*/

// Import implements the `import integration ...` command
func (r *IntegrationRunner) Import(in Request) (*Response, error) {
logger.Trace()

common := in.Common.(*flags.AssetImportCommon)

if common.Replace {
terminal.Warning("`--replace` is not supported for this command and will be ignored")
}

var integration services.Integration

if err := importUnmarshalFromRequest(in, &integration); err != nil {
return nil, err
}

if _, err := r.service.Get(integration.Name); err != nil {
if !strings.HasSuffix(err.Error(), "does not exist.\"") {
return nil, errors.New(
fmt.Sprintf("integration `%s` already exists", integration.Name),
)
}
}

res, err := r.service.Create(integration)
if err != nil {
return nil, err
}

return NewResponse(
fmt.Sprintf("Successfully imported integration `%s`", res.Name),
WithObject(res),
), nil
}
4 changes: 4 additions & 0 deletions internal/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func Error(err error, terminalNoColor bool) {
}
}

func Warning(format string, args ...interface{}) {
Display(fmt.Sprintf("WARNING: %s", format), args)
}

// DisplayTabWriter takes in a string for a table that already has tab and newlines set
// and prints a properly spaced table
// E.g. DisplayTabWriter("COL1\tCOL2\nVal1\tVal2\n", 3, false)
Expand Down
26 changes: 13 additions & 13 deletions pkg/services/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ func NewIntegrationService(iapClient client.Client) *IntegrationService {

func NewIntegration(name, integrationType string) Integration {
logger.Trace()
return Integration{Name: name, Type: integrationType}

return Integration{
Name: name,
Properties: map[string]interface{}{
"id": name,
"type": integrationType,
},
}
}

func (svc *IntegrationService) Create(in Integration) (*Integration, error) {
logger.Trace()

body := map[string]interface{}{
"properties": map[string]interface{}{
"name": in.Name,
"properties": map[string]interface{}{
"id": in.Name,
"type": in.Type,
},
"type": "Adapter",
"virtual": true,
},
}
// Make sure to set the Type and Virtual fields to these values otherwise
// the POST call will return an error
in.Type = "Adapter"
in.Virtual = true

type Response struct {
Status string `json:"status"`
Expand All @@ -63,7 +63,7 @@ func (svc *IntegrationService) Create(in Integration) (*Integration, error) {

if err := svc.client.PostRequest(&Request{
uri: "/integrations",
body: &body,
body: map[string]interface{}{"properties": in},
expectedStatusCode: http.StatusOK,
}, &res); err != nil {
return nil, err
Expand Down