diff --git a/client/integrations/clean.go b/client/integrations/clean.go new file mode 100644 index 0000000..463a72b --- /dev/null +++ b/client/integrations/clean.go @@ -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 +} diff --git a/client/integrations/executions.go b/client/integrations/executions.go index d7b1076..bd1cf98 100644 --- a/client/integrations/executions.go +++ b/client/integrations/executions.go @@ -15,6 +15,7 @@ package integrations import ( + "encoding/json" "net/url" "path" "strconv" @@ -22,7 +23,42 @@ import ( "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() @@ -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 +} diff --git a/client/integrations/integrations.go b/client/integrations/integrations.go index da6befc..3265cf7 100644 --- a/client/integrations/integrations.go +++ b/client/integrations/integrations.go @@ -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 { @@ -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) @@ -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") @@ -567,6 +577,9 @@ func Export(folder string) (err error) { apiclient.SetPrintOutput(false) respBody, err := List(maxPageSize, "", "", "") + if err != nil { + return err + } lintegrations := listintegrations{} @@ -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 diff --git a/cmd/integrations/clean.go b/cmd/integrations/clean.go new file mode 100644 index 0000000..c68bfcd --- /dev/null +++ b/cmd/integrations/clean.go @@ -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") +} diff --git a/cmd/integrations/execute.go b/cmd/integrations/execute.go new file mode 100644 index 0000000..63a4967 --- /dev/null +++ b/cmd/integrations/execute.go @@ -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") +} diff --git a/cmd/integrations/integrations.go b/cmd/integrations/integrations.go index 176010b..2078604 100644 --- a/cmd/integrations/integrations.go +++ b/cmd/integrations/integrations.go @@ -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) diff --git a/test/exec.json b/test/exec.json new file mode 100644 index 0000000..d7f275b --- /dev/null +++ b/test/exec.json @@ -0,0 +1,8 @@ +{ + "triggerId": "api_trigger/fdic_API_1", + "inputParameters": { + "fdic_certificate_number": { + "stringValue": "21314" + } + } +} \ No newline at end of file