Skip to content

Commit e6bd976

Browse files
committed
Added new command - ibmcloud sl cdn origin-remove
1 parent 122eb03 commit e6bd976

7 files changed

Lines changed: 194 additions & 4 deletions

File tree

plugin/commands/cdn/cdn.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
1616
}
1717
cobraCmd.AddCommand(NewListCommand(sl).Command)
1818
cobraCmd.AddCommand(NewDeleteCommand(sl).Command)
19+
cobraCmd.AddCommand(NewOriginRemoveCommand(sl).Command)
1920
cobraCmd.AddCommand(NewDetailCommand(sl).Command)
2021
cobraCmd.AddCommand(NewEditCommand(sl).Command)
2122
cobraCmd.AddCommand(NewCreateCommand(sl).Command)

plugin/commands/cdn/cdn_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func TestManagers(t *testing.T) {
1919

2020
var availableCommands = []string{
2121
"detail",
22+
"origin-remove",
2223
"delete",
2324
"edit",
2425
"list",
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cdn
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
7+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
8+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
10+
)
11+
12+
type OriginRemoveCommand struct {
13+
*metadata.SoftlayerCommand
14+
CdnManager managers.CdnManager
15+
Command *cobra.Command
16+
}
17+
18+
func NewOriginRemoveCommand(sl *metadata.SoftlayerCommand) *OriginRemoveCommand {
19+
thisCmd := &OriginRemoveCommand{
20+
SoftlayerCommand: sl,
21+
CdnManager: managers.NewCdnManager(sl.Session),
22+
}
23+
cobraCmd := &cobra.Command{
24+
Use: "origin-remove",
25+
Short: T("Removes an origin path for an existing CDN mapping."),
26+
Long: T("${COMMAND_NAME} sl cdn origin-remove"),
27+
Args: metadata.TwoArgs,
28+
RunE: func(cmd *cobra.Command, args []string) error {
29+
return thisCmd.Run(args)
30+
},
31+
}
32+
thisCmd.Command = cobraCmd
33+
return thisCmd
34+
}
35+
36+
func (cmd *OriginRemoveCommand) Run(args []string) error {
37+
cdnId := args[0]
38+
path := args[1]
39+
40+
_, err := cmd.CdnManager.RemoveOrigin(cdnId, path)
41+
if err != nil {
42+
return errors.NewAPIError(T("Failed to delete origin."), err.Error(), 2)
43+
}
44+
45+
cmd.UI.Ok()
46+
cmd.UI.Print(T("The origin {{.Id}} with path {{.Path}} was deleted.", map[string]interface{}{"Path": path, "Id": cdnId}))
47+
return nil
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cdn_test
2+
3+
import (
4+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
5+
. "github.com/onsi/ginkgo"
6+
. "github.com/onsi/gomega"
7+
"github.com/softlayer/softlayer-go/session"
8+
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/cdn"
10+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
12+
)
13+
14+
var _ = Describe("Cdn origin-remove", func() {
15+
var (
16+
fakeUI *terminal.FakeUI
17+
cliCommand *cdn.OriginRemoveCommand
18+
fakeSession *session.Session
19+
slCommand *metadata.SoftlayerCommand
20+
)
21+
BeforeEach(func() {
22+
fakeUI = terminal.NewFakeUI()
23+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
24+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
25+
cliCommand = cdn.NewOriginRemoveCommand(slCommand)
26+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
27+
})
28+
29+
Describe("Cdn origin-remove", func() {
30+
Context("Cdn origin-remove, Invalid Usage", func() {
31+
It("Set command without id and path", func() {
32+
err := testhelpers.RunCobraCommand(cliCommand.Command)
33+
Expect(err).To(HaveOccurred())
34+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires two argument"))
35+
})
36+
})
37+
38+
Context("Cdn origin-remove, correct use", func() {
39+
It("return cdn origin-remove", func() {
40+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456", "/path/*")
41+
Expect(err).NotTo(HaveOccurred())
42+
Expect(fakeUI.Outputs()).To(ContainSubstring("The origin"))
43+
Expect(fakeUI.Outputs()).To(ContainSubstring("was deleted."))
44+
})
45+
})
46+
})
47+
})

plugin/managers/cdn.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ import (
1414
type CdnManager interface {
1515
GetNetworkCdnMarketplaceConfigurationMapping() ([]datatypes.Container_Network_CdnMarketplace_Configuration_Mapping, error)
1616
DeleteCDN(uniqueId string) ([]datatypes.Container_Network_CdnMarketplace_Configuration_Mapping, error)
17+
RemoveOrigin(uniqueId string, path string) (string, error)
1718
GetDetailCDN(uniqueId int, mask string) (datatypes.Container_Network_CdnMarketplace_Configuration_Mapping, error)
1819
GetUsageMetrics(uniqueId int, history int, mask string) (datatypes.Container_Network_CdnMarketplace_Metrics, error)
1920
EditCDN(uniqueId int, header string, httpPort int, httpsPort int, origin string, respectHeaders string, cache string, cacheDescription string, performanceConfiguration string) (datatypes.Container_Network_CdnMarketplace_Configuration_Mapping, error)
2021
CreateCdn(hostname string, originHost string, originType string, http int, https int, bucketName string, cname string, header string, path string, ssl string) ([]datatypes.Container_Network_CdnMarketplace_Configuration_Mapping, error)
2122
}
2223

2324
type cdnManager struct {
24-
CdnService services.Network_CdnMarketplace_Configuration_Mapping
25-
Session *session.Session
25+
CdnService services.Network_CdnMarketplace_Configuration_Mapping
26+
CdnPathService services.Network_CdnMarketplace_Configuration_Mapping_Path
27+
Session *session.Session
2628
}
2729

2830
func NewCdnManager(session *session.Session) *cdnManager {
2931
return &cdnManager{
30-
CdnService: services.GetNetworkCdnMarketplaceConfigurationMappingService(session),
31-
Session: session,
32+
CdnService: services.GetNetworkCdnMarketplaceConfigurationMappingService(session),
33+
CdnPathService: services.GetNetworkCdnMarketplaceConfigurationMappingPathService(session),
34+
Session: session,
3235
}
3336
}
3437

@@ -61,6 +64,14 @@ func (a cdnManager) DeleteCDN(uniqueId string) ([]datatypes.Container_Network_Cd
6164
return a.CdnService.Mask(mask).DeleteDomainMapping(&uniqueId)
6265
}
6366

67+
/*
68+
Removes an origin path for an existing CDN mapping.
69+
https://sldn.softlayer.com/reference/services/SoftLayer_Network_CdnMarketplace_Configuration_Mapping_Path/deleteOriginPath/
70+
*/
71+
func (a cdnManager) RemoveOrigin(uniqueId string, path string) (string, error) {
72+
return a.CdnPathService.DeleteOriginPath(&uniqueId, &path)
73+
}
74+
6475
/*
6576
Gets the total number of predetermined statistics for direct display for the given mapping.
6677
https://sldn.softlayer.com/reference/services/SoftLayer_Network_CdnMarketplace_Metrics/getMappingUsageMetrics/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"Status [200]: Path \/test\/* has been deleted."

plugin/testhelpers/fake_cdn_manager.go

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)