Skip to content

Commit 6692f0d

Browse files
allmightyspiffGitHub Enterprise
authored andcommitted
Merge pull request #693 from Edson-Rios/issue692
Added new command - ibmcloud sl cdn purge
2 parents ea882b1 + 695796f commit 6692f0d

8 files changed

Lines changed: 248 additions & 6 deletions

File tree

plugin/commands/cdn/cdn.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
2121
cobraCmd.AddCommand(NewEditCommand(sl).Command)
2222
cobraCmd.AddCommand(NewOriginListCommand(sl).Command)
2323
cobraCmd.AddCommand(NewCreateCommand(sl).Command)
24+
cobraCmd.AddCommand(NewPurgeCommand(sl).Command)
2425
cobraCmd.AddCommand(NewOriginAddCommand(sl).Command)
2526
return cobraCmd
2627
}

plugin/commands/cdn/cdn_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var availableCommands = []string{
2525
"list",
2626
"origin-list",
2727
"create",
28+
"purge",
2829
"origin-add",
2930
}
3031

plugin/commands/cdn/purge.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
11+
)
12+
13+
type PurgeCommand struct {
14+
*metadata.SoftlayerCommand
15+
CdnManager managers.CdnManager
16+
Command *cobra.Command
17+
}
18+
19+
func NewPurgeCommand(sl *metadata.SoftlayerCommand) *PurgeCommand {
20+
thisCmd := &PurgeCommand{
21+
SoftlayerCommand: sl,
22+
CdnManager: managers.NewCdnManager(sl.Session),
23+
}
24+
cobraCmd := &cobra.Command{
25+
Use: "purge " + T("IDENTIFIER") + " " + T("PATH"),
26+
Short: T("Creates a purge record and also initiates the purge call."),
27+
Long: T(`${COMMAND_NAME} sl cdn purge
28+
Example:
29+
${COMMAND_NAME} sl cdn purge 9779455 /article/file.txt"`),
30+
Args: metadata.TwoArgs,
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
return thisCmd.Run(args)
33+
},
34+
}
35+
thisCmd.Command = cobraCmd
36+
return thisCmd
37+
}
38+
39+
func (cmd *PurgeCommand) Run(args []string) error {
40+
cdnId := args[0]
41+
path := args[1]
42+
outputFormat := cmd.GetOutputFlag()
43+
44+
datas, err := cmd.CdnManager.Purge(cdnId, path)
45+
if err != nil {
46+
return errors.NewAPIError(T("Failed to purge."), err.Error(), 2)
47+
}
48+
49+
table := cmd.UI.Table([]string{
50+
T("Date"),
51+
T("Path"),
52+
T("Saved"),
53+
T("Status"),
54+
})
55+
for _, data := range datas {
56+
table.Add(
57+
utils.FormatStringToTime(data.Date),
58+
utils.FormatStringPointer(data.Path),
59+
utils.FormatStringPointer(data.Saved),
60+
utils.FormatStringPointer(data.Status),
61+
)
62+
}
63+
utils.PrintTable(cmd.UI, table, outputFormat)
64+
65+
return nil
66+
}

plugin/commands/cdn/purge_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 purge", func() {
15+
var (
16+
fakeUI *terminal.FakeUI
17+
cliCommand *cdn.PurgeCommand
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.NewPurgeCommand(slCommand)
26+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
27+
})
28+
29+
Describe("Cdn purge", func() {
30+
Context("Cdn purge, 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 arguments."))
35+
})
36+
})
37+
38+
Context("Cdn purge, correct use", func() {
39+
It("return cdn purge", func() {
40+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456789", "/example/")
41+
Expect(err).NotTo(HaveOccurred())
42+
Expect(fakeUI.Outputs()).To(ContainSubstring("Date"))
43+
Expect(fakeUI.Outputs()).To(ContainSubstring("Path"))
44+
Expect(fakeUI.Outputs()).To(ContainSubstring("Saved"))
45+
Expect(fakeUI.Outputs()).To(ContainSubstring("Status"))
46+
Expect(fakeUI.Outputs()).To(ContainSubstring("/example/"))
47+
Expect(fakeUI.Outputs()).To(ContainSubstring("UNSAVED"))
48+
Expect(fakeUI.Outputs()).To(ContainSubstring("SUCCESS"))
49+
})
50+
It("return cdn purge in format json", func() {
51+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456789", "/example/", "--output", "json")
52+
Expect(err).NotTo(HaveOccurred())
53+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Date":`))
54+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Path": "/example/",`))
55+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Saved": "UNSAVED",`))
56+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Status": "SUCCESS"`))
57+
Expect(fakeUI.Outputs()).To(ContainSubstring(`[`))
58+
Expect(fakeUI.Outputs()).To(ContainSubstring(`{`))
59+
Expect(fakeUI.Outputs()).To(ContainSubstring(`}`))
60+
Expect(fakeUI.Outputs()).To(ContainSubstring(`]`))
61+
})
62+
})
63+
})
64+
})

plugin/managers/cdn.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@ type CdnManager interface {
2121
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)
2222
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)
2323
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)
24+
Purge(uniqueId string, path string) ([]datatypes.Container_Network_CdnMarketplace_Configuration_Cache_Purge, error)
2425
}
2526

2627
type cdnManager struct {
27-
CdnService services.Network_CdnMarketplace_Configuration_Mapping
28-
CdnPathService services.Network_CdnMarketplace_Configuration_Mapping_Path
29-
Session *session.Session
28+
CdnService services.Network_CdnMarketplace_Configuration_Mapping
29+
CdnPathService services.Network_CdnMarketplace_Configuration_Mapping_Path
30+
CdnPurgeService services.Network_CdnMarketplace_Configuration_Cache_Purge
31+
Session *session.Session
3032
}
3133

3234
func NewCdnManager(session *session.Session) *cdnManager {
3335
return &cdnManager{
34-
CdnService: services.GetNetworkCdnMarketplaceConfigurationMappingService(session),
35-
CdnPathService: services.GetNetworkCdnMarketplaceConfigurationMappingPathService(session),
36-
Session: session,
36+
CdnService: services.GetNetworkCdnMarketplaceConfigurationMappingService(session),
37+
CdnPathService: services.GetNetworkCdnMarketplaceConfigurationMappingPathService(session),
38+
CdnPurgeService: services.GetNetworkCdnMarketplaceConfigurationCachePurgeService(session),
39+
Session: session,
3740
}
3841
}
3942

@@ -270,3 +273,11 @@ func (a cdnManager) OriginAddCdn(uniqueId string, header string, path string, or
270273

271274
return a.CdnPathService.CreateOriginPath(&NewOrigin)
272275
}
276+
277+
/*
278+
This method creates a purge record in the purge table, and also initiates the create purge call.
279+
https://sldn.softlayer.com/reference/services/SoftLayer_Network_CdnMarketplace_Configuration_Cache_Purge/createPurge/
280+
*/
281+
func (a cdnManager) Purge(uniqueId string, path string) ([]datatypes.Container_Network_CdnMarketplace_Configuration_Cache_Purge, error) {
282+
return a.CdnPurgeService.CreatePurge(&uniqueId, &path)
283+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"date": "1682697987",
4+
"path": "/example/",
5+
"saved": "UNSAVED",
6+
"status": "SUCCESS"
7+
}
8+
]

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.

plugin/utils/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,13 @@ func GetPodWithClosedAnnouncement(key string, pods []datatypes.Network_Pod) stri
417417
}
418418
return "-"
419419
}
420+
421+
func FormatStringToTime(timestamp *string) string {
422+
timeInt, err := strconv.ParseInt(*timestamp, 10, 64)
423+
if err != nil {
424+
fmt.Println("Error:", err)
425+
return ""
426+
}
427+
t := time.Unix(timeInt, 0)
428+
return t.Format("2006-01-02 15:04:05")
429+
}

0 commit comments

Comments
 (0)