Exemple #1
0
func smsHandler(cmd *Cmd) (err error) {
	// New govh client
	client := govh.NewClient(OVH_APP_KEY, OVH_APP_SECRET, ck)
	// New sms ressource
	smsr, err := sms.New(client)

	switch cmd.Action {
	// List SMS servives
	// ./ovh sms listServices
	case "listServices":
		services, err := smsr.ListServices()
		if err != nil {
			dieError(err)
		}
		for _, s := range services {
			fmt.Println(s)
		}
		dieOk("")
	// Send a new SMS
	// ./ovh sms SERVICE_NAME new {JSON encoded SMS job}
	case "new":
		var job sms.NewJob
		err := json.Unmarshal([]byte(cmd.Args[3]), &job)
		if err != nil {
			dieError(err)
		}
		report, err := smsr.AddJob(cmd.Args[2], &job)
		if err != nil {
			dieError(err)
		}
		fmt.Printf("Done%s", NL)
		for _, id := range report.Ids {
			fmt.Printf("Job ID: %d%s", id, NL)
		}
		fmt.Printf("Credits removed: %d%s", report.TotalCreditsRemoved, NL)
		dieOk("")
	default:
		err = errors.New(fmt.Sprintf("This action : '%s' is not valid or not implemented yet !", strings.Join(cmd.Args, " ")))
	}
	return

}
Exemple #2
0
// getFwCmds return commands for firewall subsection
func getSmsCmds(client *govh.OvhClient) (smsCmds []cli.Command) {
	sr, err := sms.New(client)
	if err != nil {
		return
	}
	smsCmds = []cli.Command{
		{
			Name:        "listServices",
			Usage:       "Return a list of sms services ",
			Description: "ovh sms listServices" + NLTAB + "Example: ovh sms listServices",
			Action: func(c *cli.Context) {
				services, err := sr.ListServices()
				handleErrFromOvh(err)
				for _, service := range services {
					fmt.Println(service)
				}
				dieOk()
			},
		},
		{
			Name:        "send",
			Usage:       "Send an new SMS",
			Description: `ovh sms send SERVICE [--flags...]" + NLTAB + "Example: ovh sms send sms-st2-1 --sender +33979XXXX --receivers +336222XXXX +336221XXXX --message "Test from ovh-cli"`,
			Flags: []cli.Flag{
				cli.StringFlag{"sender", "", "The sender phone number in international format (+33XXXXXX for France for ex). Required.", ""},
				cli.StringSliceFlag{"receiver", &cli.StringSlice{}, "Receiver phone number. If you have multiple receivers add on --receiver flag by reciever. Requiered.", ""},
				cli.StringFlag{"message", "", "The message you want to send. Required", ""},
			},
			Action: func(c *cli.Context) {
				dieIfArgsMiss(len(c.Args()), 1)

				// sender
				sender := c.String("sender")
				if sender == "" {
					dieBadArgs("Required flag --sender is missing")
				}
				// recievers
				receivers := c.StringSlice("receiver")
				if receivers == nil {
					dieBadArgs("Required flag --receivers is missing")
				}
				/*for _, rcv := range receivers {
					fmt.Println(rcv)
				}*/

				// message
				message := c.String("message")
				if message == "" {
					dieBadArgs("Required flag --message is missing")
				}

				// ValidityPeriod
				validityPeriod := 2880

				// Class
				class := "sim"

				// Create the job
				job := &sms.SendJob{
					Sender:         sender,
					Receivers:      receivers,
					Message:        message,
					ValidityPeriod: validityPeriod,
					Class:          class,
				}
				resp, err := sr.AddJob(c.Args().First(), job)
				handleErrFromOvh(err)
				for _, id := range resp.Ids {
					fmt.Println("Job ID:", id)
				}
				fmt.Println("Invalid receivers:", strings.Join(resp.InvalidReceivers, ", "))
				fmt.Println("Valid receivers:", strings.Join(resp.ValidReceivers, ", "))
				fmt.Println("Credits removed:", fmt.Sprintf("%d", resp.TotalCreditsRemoved))
				dieDone()
			},
		},
	}
	return
}