Skip to content

Commit 3cb8c02

Browse files
allmightyspiffGitHub Enterprise
authored andcommitted
Merge pull request #682 from Edson-Rios/issue680
Added new command - ibmcloud sl cdn origin-list
2 parents d075eed + 47e0af1 commit 3cb8c02

7 files changed

Lines changed: 256 additions & 0 deletions

File tree

plugin/commands/cdn/cdn.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
1818
cobraCmd.AddCommand(NewDeleteCommand(sl).Command)
1919
cobraCmd.AddCommand(NewDetailCommand(sl).Command)
2020
cobraCmd.AddCommand(NewEditCommand(sl).Command)
21+
cobraCmd.AddCommand(NewOriginListCommand(sl).Command)
2122
cobraCmd.AddCommand(NewCreateCommand(sl).Command)
2223
cobraCmd.AddCommand(NewOriginAddCommand(sl).Command)
2324
return cobraCmd

plugin/commands/cdn/cdn_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var availableCommands = []string{
2222
"delete",
2323
"edit",
2424
"list",
25+
"origin-list",
2526
"create",
2627
"origin-add",
2728
}

plugin/commands/cdn/origin_list.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package cdn
2+
3+
import (
4+
"github.com/softlayer/softlayer-go/datatypes"
5+
"github.com/spf13/cobra"
6+
7+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/terminal"
8+
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
10+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
14+
)
15+
16+
type OriginListCommand struct {
17+
*metadata.SoftlayerCommand
18+
CdnManager managers.CdnManager
19+
Command *cobra.Command
20+
}
21+
22+
func NewOriginListCommand(sl *metadata.SoftlayerCommand) *OriginListCommand {
23+
thisCmd := &OriginListCommand{
24+
SoftlayerCommand: sl,
25+
CdnManager: managers.NewCdnManager(sl.Session),
26+
}
27+
cobraCmd := &cobra.Command{
28+
Use: "origin-list " + T("IDENTIFIER"),
29+
Short: T("List origin path for an existing CDN mapping."),
30+
Long: T("${COMMAND_NAME} sl cdn origin-list"),
31+
Args: metadata.OneArgs,
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
return thisCmd.Run(args)
34+
},
35+
}
36+
thisCmd.Command = cobraCmd
37+
return thisCmd
38+
}
39+
40+
func (cmd *OriginListCommand) Run(args []string) error {
41+
cdnId := args[0]
42+
43+
outputFormat := cmd.GetOutputFlag()
44+
45+
cdnOriginList, err := cmd.CdnManager.GetOrigins(cdnId)
46+
if err != nil {
47+
return errors.NewAPIError(T("Failed to get origins list for CDN: {{.cdnId}}.", map[string]interface{}{"cdnId": cdnId}), err.Error(), 2)
48+
}
49+
50+
PrintOriginsList(cdnOriginList, cmd.UI, outputFormat)
51+
return nil
52+
}
53+
54+
func PrintOriginsList(cdnOriginList []datatypes.Container_Network_CdnMarketplace_Configuration_Mapping_Path, ui terminal.UI, outputFormat string) {
55+
table := ui.Table([]string{
56+
T("Path"),
57+
T("Origin"),
58+
T("Http Port"),
59+
T("Https Port"),
60+
T("Status"),
61+
})
62+
for _, origin := range cdnOriginList {
63+
table.Add(
64+
utils.FormatStringPointer(origin.Path),
65+
utils.FormatStringPointer(origin.Origin),
66+
utils.FormatIntPointer(origin.HttpPort),
67+
utils.FormatIntPointer(origin.HttpsPort),
68+
utils.FormatStringPointer(origin.Status),
69+
)
70+
}
71+
utils.PrintTable(ui, table, outputFormat)
72+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 list Cdn", func() {
15+
var (
16+
fakeUI *terminal.FakeUI
17+
cliCommand *cdn.OriginListCommand
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.NewOriginListCommand(slCommand)
26+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
27+
})
28+
29+
Describe("Cdn origin list", func() {
30+
Context("Cdn origin list, Invalid Usage", func() {
31+
It("Set command with an invalid output option", func() {
32+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456789", "--output=xml")
33+
Expect(err).To(HaveOccurred())
34+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: Invalid output format, only JSON is supported now."))
35+
})
36+
37+
It("Set command with an invalid output option", func() {
38+
err := testhelpers.RunCobraCommand(cliCommand.Command)
39+
Expect(err).To(HaveOccurred())
40+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument."))
41+
})
42+
})
43+
44+
Context("Cdn origin list, correct use", func() {
45+
It("return cdn list", func() {
46+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456789")
47+
Expect(err).NotTo(HaveOccurred())
48+
Expect(fakeUI.Outputs()).To(ContainSubstring("Path"))
49+
Expect(fakeUI.Outputs()).To(ContainSubstring("Origin"))
50+
Expect(fakeUI.Outputs()).To(ContainSubstring("/example1/*"))
51+
Expect(fakeUI.Outputs()).To(ContainSubstring("123.123.123.123"))
52+
Expect(fakeUI.Outputs()).To(ContainSubstring("RUNNING"))
53+
})
54+
55+
It("return origin list in format json", func() {
56+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456789", "--output", "json")
57+
Expect(err).NotTo(HaveOccurred())
58+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Path": "/example1/*",`))
59+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Origin": "123.123.123.123",`))
60+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Http Port": "80",`))
61+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Status": "RUNNING"`))
62+
Expect(fakeUI.Outputs()).To(ContainSubstring(`[`))
63+
Expect(fakeUI.Outputs()).To(ContainSubstring(`{`))
64+
Expect(fakeUI.Outputs()).To(ContainSubstring(`}`))
65+
Expect(fakeUI.Outputs()).To(ContainSubstring(`]`))
66+
})
67+
})
68+
})
69+
})

plugin/managers/cdn.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type CdnManager interface {
1616
DeleteCDN(uniqueId string) ([]datatypes.Container_Network_CdnMarketplace_Configuration_Mapping, error)
1717
GetDetailCDN(uniqueId int, mask string) (datatypes.Container_Network_CdnMarketplace_Configuration_Mapping, error)
1818
GetUsageMetrics(uniqueId int, history int, mask string) (datatypes.Container_Network_CdnMarketplace_Metrics, error)
19+
GetOrigins(uniqueId string) ([]datatypes.Container_Network_CdnMarketplace_Configuration_Mapping_Path, 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
OriginAddCdn(uniqueId string, header string, path string, originHost string, originType string, http int, https int, cacheKey string, optimize string, dynamicPath string, dynamicPrefetch bool, dynamicCompression bool, bucketName string, fileExtension string) ([]datatypes.Container_Network_CdnMarketplace_Configuration_Mapping_Path, error)
@@ -152,6 +153,13 @@ func (a cdnManager) EditCDN(uniqueId int, header string, httpPort int, httpsPort
152153
return cdn[0], nil
153154
}
154155

156+
/*
157+
https://sldn.softlayer.com/reference/services/SoftLayer_Network_CdnMarketplace_Configuration_Mapping/listDomainMappingByUniqueId/
158+
*/
159+
func (a cdnManager) GetOrigins(uniqueId string) ([]datatypes.Container_Network_CdnMarketplace_Configuration_Mapping_Path, error) {
160+
return a.CdnPathService.ListOriginPath(&uniqueId)
161+
}
162+
155163
/*
156164
https://sldn.softlayer.com/reference/services/SoftLayer_Network_CdnMarketplace_Configuration_Mapping/createDomainMapping/
157165
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"cacheKeyQueryRule": "ignore-all",
4+
"header": "www.test.com",
5+
"httpPort": 80,
6+
"httpsPort": null,
7+
"mappingUniqueId": "401853089188535",
8+
"origin": "123.123.123.123",
9+
"originType": "HOST_SERVER",
10+
"path": "\/example1\/*",
11+
"performanceConfiguration": "General web delivery",
12+
"status": "RUNNING"
13+
},
14+
{
15+
"cacheKeyQueryRule": "ignore-all",
16+
"header": "www.origin.com",
17+
"httpPort": 80,
18+
"httpsPort": null,
19+
"mappingUniqueId": "401853089188535",
20+
"origin": "123.123.123.123",
21+
"originType": "HOST_SERVER",
22+
"path": "\/example2\/*",
23+
"performanceConfiguration": "General web delivery",
24+
"status": "RUNNING"
25+
}
26+
]

plugin/testhelpers/fake_cdn_manager.go

Lines changed: 79 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)