Пример #1
0
func main() {
	// 1. Retrieve login and endpoint information
	email := flag.String("e", "", "Login email")
	pwd := flag.String("p", "", "Login password")
	account := flag.Int("a", 0, "Account id")
	host := flag.String("h", "us-3.rightscale.com", "RightScale API host")
	insecure := flag.Bool("insecure", false, "Bypass certificate check, used for testing")
	cat := flag.String("cat", "", "Path to CAT file")
	flag.Parse()
	if *email == "" {
		fail("Login email required")
	}
	if *pwd == "" {
		fail("Login password required")
	}
	if *account == 0 {
		fail("Account id required")
	}
	if *host == "" {
		fail("Host required")
	}
	if *insecure {
		httpclient.NoCertCheck = true
	}
	if *cat == "" {
		fail("Path to CAT required")
	}
	if _, err := os.Stat(*cat); err != nil {
		fail("Can't find file %s", *cat)
	}

	// 2. Setup client using basic auth
	auth := rsapi.NewBasicAuthenticator(*email, *pwd, *account)
	ssAuth := rsapi.NewSSAuthenticator(auth, *account)
	client := ssd.New(*host, ssAuth)
	if err := client.CanAuthenticate(); err != nil {
		fail("invalid credentials: %s", err)
	}

	// 3. Make execution index call using expanded view
	file, err := os.Open(*cat)
	if err != nil {
		fail("Could not open %s", *cat)
	}
	name := filepath.Base(*cat)
	l := client.TemplateLocator(fmt.Sprintf("/designer/collections/%d/templates", *account))
	upload := rsapi.FileUpload{Name: name, Filename: name, Reader: file}
	t, err := l.Create(&upload)
	if err != nil {
		fail("failed to create template: %s", err)
	}
	created, err := t.Show(rsapi.ApiParams{})
	if err != nil {
		fail("failed to retrieve created template: %s", err)
	}
	fmt.Fprintf(osStdout, "%s created at %s by %s\n",
		created.Name, created.Timestamps.CreatedAt, created.CreatedBy.Name)
}
Пример #2
0
func NewClient(apiHost, ssHost string, account int, token string) (*Client, error) {
	apiAuth := rsapi.NewOAuthAuthenticator(token, account)
	apiAuth.SetHost(apiHost)

	if err := apiAuth.CanAuthenticate(apiHost); err != nil {
		return nil, fmt.Errorf("invalid credentials: %s\n", err)
	}

	ssAuth := rsapi.NewSSAuthenticator(apiAuth, account)
	return &Client{
		account:  account,
		designer: ssd.New(ssHost, ssAuth),
		manager:  ssm.New(ssHost, ssAuth),
	}, nil
}