Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #39 from srinandan/clean
Browse files Browse the repository at this point in the history
Clean unused integrations execute integrations
  • Loading branch information
srinandan authored Nov 3, 2022
2 parents c02b1a7 + 8889f4f commit 37a7e59
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 1 deletion.
70 changes: 70 additions & 0 deletions client/integrations/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package integrations

import (
"encoding/json"
"fmt"

"github.com/apigee/apigeecli/clilog"
"github.com/srinandan/integrationcli/apiclient"
)

func Clean(name string, reportOnly bool, keepList []string) (err error) {

var listOfVersions []basicIntegrationVersion
var nextPage string

apiclient.SetPrintOutput(false)
for {
respBody, err := ListVersions(name, -1, nextPage, "", "", false, false, true)
if err != nil {
return err
}
iversions := listbasicIntegrationVersions{}
err = json.Unmarshal(respBody, &iversions)
if err != nil {
return err
}

listOfVersions = append(listOfVersions, iversions.BasicIntegrationVersions...)
if iversions.NextPageToken == "" {
break
}
nextPage = iversions.NextPageToken
}

if len(listOfVersions) == 0 {
clilog.Warning.Println("no integration versions where found")
return nil
}

for _, iversion := range listOfVersions {
if iversion.State != "ACTIVE" {
if reportOnly {
fmt.Println("[REPORT]: Integration '" + name + "' Version: " + iversion.Version + " and Snapshot " + iversion.SnapshotNumber + " can be cleaned")
} else {
_, err = Delete(name, iversion.Version)
if err != nil {
return err
}
}
}
}

apiclient.SetPrintOutput(true)

return nil
}
51 changes: 50 additions & 1 deletion client/integrations/executions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,50 @@
package integrations

import (
"encoding/json"
"net/url"
"path"
"strconv"

"github.com/srinandan/integrationcli/apiclient"
)

// List all executions
type execute struct {
TriggerId string `json:"triggerId,omitempty"`
DoNotPropagateError bool `json:"doNotPropagateError,omitempty"`
RequestId string `json:"requestId,omitempty"`
InputParameters map[string]inputparameter `json:"inputParameters,omitempty"`
}

type inputparameter struct {
StringValue *string `json:"stringValue,omitempty"`
IntValue *string `json:"intValue,omitempty"`
DoubleValue *float32 `json:"doubleValue,omitempty"`
BooleanValue *bool `json:"booleanValue,omitempty"`
JsonValue *string `json:"jsonValue,omitempty"`
StringArray *stringarray `json:"stringarray,omitempty"`
IntArray *intarray `json:"intarray,omitempty"`
DoubleArray *doublearray `json:"doubleArray,omitempty"`
BooleanArray *booleanarray `json:"booleanArray,omitempty"`
}

type stringarray struct {
StringValues []string `json:"stringValues,omitempty"`
}

type intarray struct {
IntValues []string `json:"intValues,omitempty"`
}

type doublearray struct {
DoubleValues []float32 `json:"doubleValues,omitempty"`
}

type booleanarray struct {
BooleanValues []bool
}

// ListExecutions lists all executions
func ListExecutions(name string, pageSize int, pageToken string, filter string, orderBy string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.GetBaseIntegrationURL())
q := u.Query()
Expand All @@ -44,3 +80,16 @@ func ListExecutions(name string, pageSize int, pageToken string, filter string,
respBody, err = apiclient.HttpClient(apiclient.GetPrintOutput(), u.String())
return respBody, err
}

// Execute
func Execute(name string, content []byte) (respBody []byte, err error) {
e := execute{}
if err = json.Unmarshal(content, &e); err != nil {
return nil, err
}

u, _ := url.Parse(apiclient.GetBaseIntegrationURL())
u.Path = path.Join(u.Path, "integrations", name+":execute")
respBody, err = apiclient.HttpClient(apiclient.GetPrintOutput(), u.String(), string(content))
return respBody, err
}
17 changes: 17 additions & 0 deletions client/integrations/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type listbasicIntegrationVersions struct {
type basicIntegrationVersion struct {
Version string `json:"version,omitempty"`
SnapshotNumber string `json:"snapshotNumber,omitempty"`
State string `json:"state,omitempty"`
}

type listintegrations struct {
Expand Down Expand Up @@ -278,6 +279,7 @@ func ListVersions(name string, pageSize int, pageToken string, filter string, or
basicIVer := basicIntegrationVersion{}
basicIVer.SnapshotNumber = iVer.SnapshotNumber
basicIVer.Version = getVersion(iVer.Name)
basicIVer.State = iVer.State
listBIvers.BasicIntegrationVersions = append(listBIvers.BasicIntegrationVersions, basicIVer)
}
newResp, err := json.Marshal(listBIvers)
Expand Down Expand Up @@ -444,6 +446,14 @@ func GetByUserlabel(name string, userLabel string) ([]byte, error) {
return Get(name, version, false)
}

// Delete - THIS IS UNIMPLEMENTED!!!
func Delete(name string, version string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.GetBaseIntegrationURL())
u.Path = path.Join(u.Path, "integrations", name, "versions", version)
//respBody, err = apiclient.HttpClient(apiclient.GetPrintOutput(), u.String(), "", "DELETE")
return respBody, fmt.Errorf("not implemented")
}

// Deactivate
func Deactivate(name string, version string) (respBody []byte, err error) {
return changeState(name, version, "", ":deactivate")
Expand Down Expand Up @@ -567,6 +577,9 @@ func Export(folder string) (err error) {
apiclient.SetPrintOutput(false)

respBody, err := List(maxPageSize, "", "", "")
if err != nil {
return err
}

lintegrations := listintegrations{}

Expand Down Expand Up @@ -598,6 +611,10 @@ func Export(folder string) (err error) {
// batchExport
func batchExport(folder string, nextPageToken string) (err error) {
respBody, err := List(maxPageSize, nextPageToken, "", "")
if err != nil {
return err
}

lintegrations := listintegrations{}
if err = json.Unmarshal(respBody, &lintegrations); err != nil {
return err
Expand Down
52 changes: 52 additions & 0 deletions cmd/integrations/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package integrations

import (
"github.com/spf13/cobra"
"github.com/srinandan/integrationcli/apiclient"
"github.com/srinandan/integrationcli/client/integrations"
)

// CleanCmd to delete integration versions
var CleanCmd = &cobra.Command{
Use: "clean",
Short: "Deletes undeployed/unused versions of an Integration",
Long: "Deletes undeployed/unused versions of an Integration",
Args: func(cmd *cobra.Command, args []string) (err error) {
if err = apiclient.SetRegion(region); err != nil {
return err
}

return apiclient.SetProjectID(project)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
return integrations.Clean(name, reportOnly, keepList)
},
}

var reportOnly bool
var keepList []string

func init() {
CleanCmd.Flags().StringVarP(&name, "name", "n",
"", "Integration name")
CleanCmd.Flags().BoolVarP(&reportOnly, "report", "",
true, "Report which integration snapshots will be deleted")
CleanCmd.Flags().StringArrayVarP(&keepList, "keepList", "k",
[]string{}, "List of snapshots to keep, -k 1 -k 2")

_ = CleanCmd.MarkFlagRequired("name")
}
65 changes: 65 additions & 0 deletions cmd/integrations/execute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package integrations

import (
"io/ioutil"
"os"

"github.com/srinandan/integrationcli/apiclient"
"github.com/srinandan/integrationcli/client/integrations"

"github.com/spf13/cobra"
)

// ExecuteCmd an Integration
var ExecuteCmd = &cobra.Command{
Use: "execute",
Short: "Execute an integration",
Long: "execute an integration",
Args: func(cmd *cobra.Command, args []string) (err error) {
if err = apiclient.SetRegion(region); err != nil {
return err
}
return apiclient.SetProjectID(project)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {

if _, err := os.Stat(executionFile); os.IsNotExist(err) {
return err
}

content, err := ioutil.ReadFile(executionFile)
if err != nil {
return err
}

_, err = integrations.Execute(name, content)
return

},
}

var executionFile string

func init() {
ExecuteCmd.Flags().StringVarP(&name, "name", "n",
"", "Integration flow name")
ExecuteCmd.Flags().StringVarP(&executionFile, "file", "f",
"", "Integration flow JSON file path")

_ = ExecuteCmd.MarkFlagRequired("name")
_ = ExecuteCmd.MarkFlagRequired("file")
}
2 changes: 2 additions & 0 deletions cmd/integrations/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func init() {

Cmd.AddCommand(ListCmd)
Cmd.AddCommand(VerCmd)
Cmd.AddCommand(CleanCmd)
Cmd.AddCommand(ExecuteCmd)
Cmd.AddCommand(ExecCmd)
Cmd.AddCommand(ExportCmd)
Cmd.AddCommand(ImportCmd)
Expand Down
8 changes: 8 additions & 0 deletions test/exec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"triggerId": "api_trigger/fdic_API_1",
"inputParameters": {
"fdic_certificate_number": {
"stringValue": "21314"
}
}
}

0 comments on commit 37a7e59

Please sign in to comment.