Skip to content

Commit 9e4ea60

Browse files
committed
Added new command - ibmcloud sl cdn purge
1 parent 122eb03 commit 9e4ea60

8 files changed

Lines changed: 249 additions & 4 deletions

File tree

plugin/commands/cdn/cdn.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
1919
cobraCmd.AddCommand(NewDetailCommand(sl).Command)
2020
cobraCmd.AddCommand(NewEditCommand(sl).Command)
2121
cobraCmd.AddCommand(NewCreateCommand(sl).Command)
22+
cobraCmd.AddCommand(NewPurgeCommand(sl).Command)
2223
return cobraCmd
2324
}
2425

plugin/commands/cdn/cdn_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var availableCommands = []string{
2323
"edit",
2424
"list",
2525
"create",
26+
"purge",
2627
}
2728

2829
// This test suite exists to make sure commands don't get accidently removed from the actionBindings

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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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("2023-04-28 12:06:27"))
47+
Expect(fakeUI.Outputs()).To(ContainSubstring("/example/"))
48+
Expect(fakeUI.Outputs()).To(ContainSubstring("UNSAVED"))
49+
Expect(fakeUI.Outputs()).To(ContainSubstring("SUCCESS"))
50+
})
51+
It("return cdn purge in format json", func() {
52+
err := testhelpers.RunCobraCommand(cliCommand.Command, "123456789", "/example/", "--output", "json")
53+
Expect(err).NotTo(HaveOccurred())
54+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Date": "2023-04-28 12:06:27",`))
55+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Path": "/example/",`))
56+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Saved": "UNSAVED",`))
57+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"Status": "SUCCESS"`))
58+
Expect(fakeUI.Outputs()).To(ContainSubstring(`[`))
59+
Expect(fakeUI.Outputs()).To(ContainSubstring(`{`))
60+
Expect(fakeUI.Outputs()).To(ContainSubstring(`}`))
61+
Expect(fakeUI.Outputs()).To(ContainSubstring(`]`))
62+
})
63+
})
64+
})
65+
})

plugin/managers/cdn.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package managers
22

33
import (
4+
"fmt"
45
"strconv"
56
"strings"
67
"time"
@@ -18,17 +19,20 @@ type CdnManager interface {
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)
22+
Purge(uniqueId string, path string) ([]datatypes.Container_Network_CdnMarketplace_Configuration_Cache_Purge, error)
2123
}
2224

2325
type cdnManager struct {
24-
CdnService services.Network_CdnMarketplace_Configuration_Mapping
25-
Session *session.Session
26+
CdnService services.Network_CdnMarketplace_Configuration_Mapping
27+
CdnPurgeService services.Network_CdnMarketplace_Configuration_Cache_Purge
28+
Session *session.Session
2629
}
2730

2831
func NewCdnManager(session *session.Session) *cdnManager {
2932
return &cdnManager{
30-
CdnService: services.GetNetworkCdnMarketplaceConfigurationMappingService(session),
31-
Session: session,
33+
CdnService: services.GetNetworkCdnMarketplaceConfigurationMappingService(session),
34+
CdnPurgeService: services.GetNetworkCdnMarketplaceConfigurationCachePurgeService(session),
35+
Session: session,
3236
}
3337
}
3438

@@ -199,3 +203,12 @@ func (a cdnManager) CreateCdn(hostname string, originHost string, originType str
199203

200204
return a.CdnService.CreateDomainMapping(&NewOrigin)
201205
}
206+
207+
/*
208+
This method creates a purge record in the purge table, and also initiates the create purge call.
209+
https://sldn.softlayer.com/reference/services/SoftLayer_Network_CdnMarketplace_Configuration_Cache_Purge/createPurge/
210+
*/
211+
func (a cdnManager) Purge(uniqueId string, path string) ([]datatypes.Container_Network_CdnMarketplace_Configuration_Cache_Purge, error) {
212+
path = fmt.Sprintf("/%s", path)
213+
return a.CdnPurgeService.CreatePurge(&uniqueId, &path)
214+
}
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)