Skip to content

Commit

Permalink
Merge pull request #125 from Peripli/share-instance
Browse files Browse the repository at this point in the history
share/unshare commands
  • Loading branch information
sigalmaya authored May 13, 2021
2 parents 0a4ccae + ce2802e commit c133676
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 28 deletions.
6 changes: 3 additions & 3 deletions internal/cmd/instance/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (trc *TransferCmd) Prepare(prepare cmd.PrepareFunc) *cobra.Command {
}

result.Flags().BoolVarP(&trc.force, "force", "f", false, "Force transfer without confirmation")
result.Flags().StringVarP(&trc.instanceID, "id", "", "", "Id of the instance. Required in case when there are instances with same name")
result.Flags().StringVarP(&trc.instanceID, "id", "", "", cmd.INSTANCE_ID_DESCRIPTION)
result.Flags().StringVarP(&trc.fromPlatformID, "from", "", "", "ID of the platform from which you want to move the instance")
result.Flags().StringVarP(&trc.toPlatformID, "to", "", "", "ID of the platform to which you want to move the instance")
cmd.AddFormatFlag(result.Flags())
Expand Down Expand Up @@ -102,11 +102,11 @@ func (trc *TransferCmd) Run() error {
return err
}
if len(instances.ServiceInstances) == 0 {
return fmt.Errorf("no instances found with name %s", trc.instanceName)
return fmt.Errorf(cmd.NO_INSTANCES_FOUND, trc.instanceName)
}

if len(instances.ServiceInstances) > 1 {
return fmt.Errorf("more than 1 instance found with name %s. Use --id flag to specify one", trc.instanceName)
return fmt.Errorf(cmd.FOUND_TOO_MANY_INSTANCES, trc.instanceName, "transfer")
}

trc.instanceID = instances.ServiceInstances[0].ID
Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/instance/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"bytes"
"encoding/json"
"errors"

"fmt"
"github.com/Peripli/service-manager-cli/internal/cmd"
"github.com/Peripli/service-manager-cli/pkg/smclient/smclientfakes"
"github.com/Peripli/service-manager-cli/pkg/types"
Expand Down Expand Up @@ -120,7 +120,7 @@ var _ = Describe("Transfer Command test", func() {
Context("when no instance id is provided", func() {
It("should require flag for instance id", func() {
err := invalidTransferCommandExecution("instance-name", "--from", "from_platform", "--to", "to_platform")
Expect(err.Error()).To(Equal("more than 1 instance found with name instance-name. Use --id flag to specify one"))
Expect(err.Error()).To(Equal(fmt.Sprintf(cmd.FOUND_TOO_MANY_INSTANCES,"instance-name","transfer")))
})
})

Expand All @@ -141,7 +141,8 @@ var _ = Describe("Transfer Command test", func() {

It("should fail to transfer", func() {
err := invalidTransferCommandExecution("no-instance", "--from", "from_platform", "--to", "to_platform")
Expect(err.Error()).To(Equal("no instances found with name no-instance"))
message:=fmt.Sprintf(cmd.NO_INSTANCES_FOUND,"no-instance")
Expect(err.Error()).To(Equal(message))
})
})

Expand Down
18 changes: 9 additions & 9 deletions internal/cmd/instance/update_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ import (

type UpdateCmd struct {
*cmd.Context
instance types.ServiceInstance
instanceName string
planName string
parametersJSON string
outputFormat output.Format
instance types.ServiceInstance
instanceName string
planName string
parametersJSON string
outputFormat output.Format
}


func NewUpdateInstanceCmd(context *cmd.Context) *UpdateCmd {
return &UpdateCmd{Context: context,instance: types.ServiceInstance{}}
return &UpdateCmd{Context: context, instance: types.ServiceInstance{}}
}

// Prepare returns cobra command
Expand Down Expand Up @@ -81,11 +80,12 @@ func (uc *UpdateCmd) Run() error {
return err
}
if len(instances.ServiceInstances) == 0 {
return fmt.Errorf("no instances found with name %s", uc.instanceName)
return fmt.Errorf(cmd.NO_INSTANCES_FOUND, uc.instanceName)
}

if len(instances.ServiceInstances) > 1 {
return fmt.Errorf("more than 1 instance found with name %s. Use --id flag to specify one", uc.instanceName)
return fmt.Errorf(cmd.FOUND_TOO_MANY_INSTANCES, uc.instanceName, "update")

}
instanceBeforeUpdate = &instances.ServiceInstances[0]

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/instance/update_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ var _ = Describe("update instance command test", func() {
Context("by name", func() {
It("should return an error", func() {
err := invalidUpdateInstanceCommandExecution("instance-name", "--new-name", "new name")
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("no instances found with name %s", "instance-name")))
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(cmd.NO_INSTANCES_FOUND, "instance-name")))
})

})
Expand Down Expand Up @@ -203,7 +203,7 @@ var _ = Describe("update instance command test", func() {
})
It("should return an error", func() {
err := invalidUpdateInstanceCommandExecution("instance-name", "--new-name", "new name")
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("more than 1 instance found with name %s", "instance-name")))
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(cmd.FOUND_TOO_MANY_INSTANCES, "instance-name","update")))
})

})
Expand Down
120 changes: 120 additions & 0 deletions internal/cmd/instance/update_sharing_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2018 The Service Manager Authors
*
* 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 instance

import (
"github.com/Peripli/service-manager-cli/internal/cmd"
"github.com/Peripli/service-manager-cli/internal/output"
"github.com/Peripli/service-manager-cli/pkg/query"
"github.com/Peripli/service-manager-cli/pkg/types"
"github.com/Peripli/service-manager/pkg/web"

"fmt"

"github.com/spf13/cobra"
)

type UpdateSharingCmd struct {
*cmd.Context
instanceName string
instanceID string
outputFormat output.Format
share bool
action string
}

// NewUpdateSharingCmd returns new share/unshare instance command with context
func NewUpdateSharingCmd(context *cmd.Context, share bool) *UpdateSharingCmd {
return &UpdateSharingCmd{Context: context, share: share}
}

// Prepare returns cobra command
func (shc *UpdateSharingCmd) Prepare(prepare cmd.PrepareFunc) *cobra.Command {
result := &cobra.Command{
PreRunE: prepare(shc, shc.Context),
RunE: cmd.RunE(shc),
}
if shc.share {
shc.action = "share"
result.Use = "share-instance [name] --id service-instance-id "
result.Short = "Share a service instance"
result.Long = `Share a service instance so that it can be consumed from various platforms in your subaccount.
Instance can be shared only if it was created with the plan that supports instance sharing. For more information, see the documentation of the service whose instance you want to share. To check if the service instance was created with a shareable plan, use 'smctl list-plans'.`
} else {
shc.action = "unshare"
result.Use = "unshare-instance [name] --id service-instance-id "
result.Short = "Unshare a service instance"
result.Long = `Unshare a service instance to disable its consumption from any but the original platform in which it was created in your subaccount. If an instance you want to unshare has references, an error is returned`
}
result.Flags().StringVarP(&shc.instanceID, "id", "", "", cmd.INSTANCE_ID_DESCRIPTION)
cmd.AddFormatFlag(result.Flags())
return result
}

// Validate validates command's arguments
func (shc *UpdateSharingCmd) Validate(args []string) error {
if len(args) < 1 {
return fmt.Errorf("service instance name is required")
}
shc.instanceName = args[0]
return nil
}

// Run runs the command's logic
func (shc *UpdateSharingCmd) Run() error {
if shc.instanceID == "" {
instances, err := shc.Client.ListInstances(&query.Parameters{
FieldQuery: []string{
fmt.Sprintf("name eq '%s'", shc.instanceName),
},
GeneralParams: shc.Parameters.GeneralParams,
})
if err != nil {
return err
}
if len(instances.ServiceInstances) == 0 {
return fmt.Errorf(cmd.NO_INSTANCES_FOUND, shc.instanceName)
}

if len(instances.ServiceInstances) > 1 {
return fmt.Errorf(cmd.FOUND_TOO_MANY_INSTANCES, shc.instanceName, shc.action)
}

shc.instanceID = instances.ServiceInstances[0].ID
}
shc.Parameters.GeneralParams = append(shc.Parameters.GeneralParams, fmt.Sprintf("%s=%s", web.QueryParamAsync, "false"))
shared:=new(bool)
*shared = shc.share
resultInstance, _, err := shc.Client.UpdateInstance(shc.instanceID, &types.ServiceInstance{
Shared: shared,
}, &query.Parameters{
GeneralParams: shc.Parameters.GeneralParams,
})
if err != nil {
output.PrintMessage(shc.Output, fmt.Sprintf("Couldn't %s the service instance. ", shc.action))
return err
}

output.PrintServiceManagerObject(shc.Output, shc.outputFormat, resultInstance)
output.Println(shc.Output)
return nil
}

// SetOutputFormat set output format
func (shc *UpdateSharingCmd) SetOutputFormat(format output.Format) {
shc.outputFormat = format
}
Loading

0 comments on commit c133676

Please sign in to comment.