|
| 1 | +package cdn |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/terminal" |
| 5 | + "github.com/softlayer/softlayer-go/datatypes" |
| 6 | + "github.com/spf13/cobra" |
| 7 | + |
| 8 | + "github.ibm.com/SoftLayer/softlayer-cli/plugin/errors" |
| 9 | + . "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n" |
| 10 | + "github.ibm.com/SoftLayer/softlayer-cli/plugin/managers" |
| 11 | + "github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata" |
| 12 | + "github.ibm.com/SoftLayer/softlayer-cli/plugin/utils" |
| 13 | +) |
| 14 | + |
| 15 | +type CreateCommand struct { |
| 16 | + *metadata.SoftlayerCommand |
| 17 | + CdnManager managers.CdnManager |
| 18 | + Command *cobra.Command |
| 19 | + HostName string |
| 20 | + OriginHost string |
| 21 | + OriginType string |
| 22 | + Http int |
| 23 | + Https int |
| 24 | + BucketName string |
| 25 | + CName string |
| 26 | + Header string |
| 27 | + Path string |
| 28 | + Ssl string |
| 29 | +} |
| 30 | + |
| 31 | +func NewCreateCommand(sl *metadata.SoftlayerCommand) *CreateCommand { |
| 32 | + thisCmd := &CreateCommand{ |
| 33 | + SoftlayerCommand: sl, |
| 34 | + CdnManager: managers.NewCdnManager(sl.Session), |
| 35 | + } |
| 36 | + cobraCmd := &cobra.Command{ |
| 37 | + Use: "create", |
| 38 | + Short: T("Create a new CDN domain mapping"), |
| 39 | + Long: T(`${COMMAND_NAME} sl cdn create |
| 40 | +Example: |
| 41 | +${COMMAND_NAME} sl cdn create --hostname www.example.com --origin 123.45.67.8 --http 80`), |
| 42 | + Args: metadata.NoArgs, |
| 43 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 44 | + return thisCmd.Run(args) |
| 45 | + }, |
| 46 | + } |
| 47 | + cobraCmd.Flags().StringVar(&thisCmd.HostName, "hostname", "", T("To route requests to your website, enter the hostname for your website, for example, www.example.com or app.example.com. [required]")) |
| 48 | + cobraCmd.Flags().StringVar(&thisCmd.OriginHost, "origin", "", T("Your server IP address or hostname. [required]")) |
| 49 | + cobraCmd.Flags().StringVar(&thisCmd.OriginType, "origin-type", "server", T("The origin type. [Permit: server, storage] Note: If OriginType is storage then OriginHost is take as Endpoint")) |
| 50 | + cobraCmd.Flags().IntVar(&thisCmd.Http, "http", 0, T("Http port")) |
| 51 | + cobraCmd.Flags().IntVar(&thisCmd.Https, "https", 0, T("Https port")) |
| 52 | + cobraCmd.Flags().StringVar(&thisCmd.BucketName, "bucket-name", "", T("Bucket name")) |
| 53 | + cobraCmd.Flags().StringVar(&thisCmd.CName, "cname", "", T("Enter a globally unique subdomain. The full URL becomes the CNAME we use to configure your DNS. If no value is entered, we will generate a CNAME for you.")) |
| 54 | + cobraCmd.Flags().StringVar(&thisCmd.Header, "header", "", T("The edge server uses the host header in the HTTP header to communicate with the Origin host. It defaults to Hostname.")) |
| 55 | + cobraCmd.Flags().StringVar(&thisCmd.Path, "path", "", T("Give a path relative to the domain provided, which can be used to reach this Origin. For example, 'articles/video' => 'www.example.com/articles/video")) |
| 56 | + cobraCmd.Flags().StringVar(&thisCmd.Ssl, "ssl", "dvSan", T("A DV SAN Certificate allows HTTPS traffic over your personal domain, but it requires a domain validation to prove ownership. A wildcard certificate allows HTTPS traffic only when using the CNAME given. [Permit: dvSan, wilcard]")) |
| 57 | + |
| 58 | + //#nosec G104 -- This is a false positive |
| 59 | + cobraCmd.MarkFlagRequired("hostname") |
| 60 | + //#nosec G104 -- This is a false positive |
| 61 | + cobraCmd.MarkFlagRequired("origin") |
| 62 | + thisCmd.Command = cobraCmd |
| 63 | + return thisCmd |
| 64 | +} |
| 65 | + |
| 66 | +func (cmd *CreateCommand) Run(args []string) error { |
| 67 | + if cmd.Http == 0 && cmd.Https == 0 { |
| 68 | + return errors.NewMissingInputError("http or https") |
| 69 | + } |
| 70 | + if cmd.OriginType != "server" && cmd.OriginType != "storage" { |
| 71 | + return errors.NewInvalidUsageError("--origintype") |
| 72 | + } |
| 73 | + if cmd.Ssl != "dvSan" && cmd.Ssl != "wilcard" && cmd.Ssl != "" { |
| 74 | + return errors.NewInvalidUsageError("--ssl") |
| 75 | + } |
| 76 | + |
| 77 | + outputFormat := cmd.GetOutputFlag() |
| 78 | + |
| 79 | + newCdn, err := cmd.CdnManager.CreateCdn(cmd.HostName, cmd.OriginHost, cmd.OriginType, cmd.Http, cmd.Https, cmd.BucketName, cmd.CName, cmd.Header, cmd.Path, cmd.Ssl) |
| 80 | + if err != nil { |
| 81 | + return errors.NewAPIError(T("Failed to create a CDN."), err.Error(), 2) |
| 82 | + } |
| 83 | + |
| 84 | + PrintCndCreated(cmd.UI, newCdn, outputFormat) |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func PrintCndCreated(ui terminal.UI, cdn []datatypes.Container_Network_CdnMarketplace_Configuration_Mapping, outputFormat string) { |
| 89 | + table := ui.Table([]string{ |
| 90 | + T("Name"), |
| 91 | + T("Value"), |
| 92 | + }) |
| 93 | + if len(cdn) > 0 { |
| 94 | + table.Add(T("CDN Unique ID"), utils.FormatStringPointer(cdn[0].UniqueId)) |
| 95 | + if cdn[0].BucketName != nil { |
| 96 | + table.Add(T("Bucket Name"), utils.FormatStringPointer(cdn[0].BucketName)) |
| 97 | + } |
| 98 | + table.Add(T("Hostname"), utils.FormatStringPointer(cdn[0].Domain)) |
| 99 | + table.Add(T("Header"), utils.FormatStringPointer(cdn[0].Header)) |
| 100 | + table.Add(T("IBM CNAME"), utils.FormatStringPointer(cdn[0].Cname)) |
| 101 | + table.Add(T("Akamai CNAME"), utils.FormatStringPointer(cdn[0].AkamaiCname)) |
| 102 | + table.Add(T("Origin Host"), utils.FormatStringPointer(cdn[0].OriginHost)) |
| 103 | + table.Add(T("Origin Type"), utils.FormatStringPointer(cdn[0].OriginType)) |
| 104 | + table.Add(T("Protocol"), utils.FormatStringPointer(cdn[0].Protocol)) |
| 105 | + table.Add(T("Http Port"), utils.FormatIntPointer(cdn[0].HttpPort)) |
| 106 | + table.Add(T("Https Port"), utils.FormatIntPointer(cdn[0].HttpsPort)) |
| 107 | + table.Add(T("Certificate Type"), utils.FormatStringPointer(cdn[0].CertificateType)) |
| 108 | + table.Add(T("Provider"), utils.FormatStringPointer(cdn[0].VendorName)) |
| 109 | + table.Add(T("Path"), utils.FormatStringPointer(cdn[0].Path)) |
| 110 | + } |
| 111 | + utils.PrintTable(ui, table, outputFormat) |
| 112 | +} |
0 commit comments