當前位置: 首頁>>代碼示例>>Golang>>正文


Golang api.NewScalewayAPI函數代碼示例

本文整理匯總了Golang中github.com/scaleway/scaleway-cli/pkg/api.NewScalewayAPI函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewScalewayAPI函數的具體用法?Golang NewScalewayAPI怎麽用?Golang NewScalewayAPI使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewScalewayAPI函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: RunLogin

// RunLogin is the handler for 'scw login'
func RunLogin(ctx CommandContext, args LoginArgs) error {
	if config, cfgErr := config.GetConfig(); cfgErr == nil {
		if TestConnection, err := api.NewScalewayAPI(config.Organization, config.Token, scwversion.UserAgent(), "", clilogger.SetupLogger); err == nil {
			if user, err := TestConnection.GetUser(); err == nil {
				fmt.Println("You are already logged as", user.Fullname)
			}
		}
	}

	if args.Organization == "" || args.Token == "" {
		var err error

		args.Organization, args.Token, err = connectAPI()
		if err != nil {
			return err
		}
	}

	cfg := &config.Config{
		Organization: strings.Trim(args.Organization, "\n"),
		Token:        strings.Trim(args.Token, "\n"),
	}

	apiConnection, err := api.NewScalewayAPI(cfg.Organization, cfg.Token, scwversion.UserAgent(), "", clilogger.SetupLogger)
	if err != nil {
		return fmt.Errorf("Unable to create ScalewayAPI: %s", err)
	}
	err = apiConnection.CheckCredentials()
	if err != nil {
		return fmt.Errorf("Unable to contact ScalewayAPI: %s", err)
	}
	if !args.SkipSSHKey {
		if err = selectKey(&args); err != nil {
			logrus.Errorf("Unable to select a key: %v", err)
		} else {
			if args.SSHKey != "" {
				uploadSSHKeys(apiConnection, args.SSHKey)
			}
		}
	}
	name := "."
	user, err := apiConnection.GetUser()
	if err == nil {
		name = "as " + user.Fullname + "."
	}
	fmt.Println("")
	fmt.Println("You are now authenticated on Scaleway.com", name)
	fmt.Println("You can list your existing servers using `scw ps` or create a new one using `scw run ubuntu-xenial`.")
	fmt.Println("You can get a list of all available commands using `scw -h` and get more usage examples on github.com/scaleway/scaleway-cli.")
	fmt.Println("Happy cloud riding.")
	return cfg.Save()
}
開發者ID:QuentinPerez,項目名稱:scaleway-cli,代碼行數:53,代碼來源:login.go

示例2: getOrganization

func getOrganization(token string, email string) (string, error) {
	FakeConnection, err := api.NewScalewayAPI("", token, scwversion.UserAgent(), "", clilogger.SetupLogger)
	if err != nil {
		return "", fmt.Errorf("Unable to create a fake ScalewayAPI: %s", err)
	}
	data, err := FakeConnection.GetOrganization()
	if err != nil {
		return "", err
	}

	orgaID := ""

	for _, orga := range data.Organizations {
		for _, user := range orga.Users {
			if user.Email == email {
				for i := range user.Organizations {
					if user.Organizations[i].Name != "OCS" {
						orgaID = user.Organizations[i].ID
						goto exit
					}
				}
			}
		}
	}
	if orgaID == "" {
		return "", fmt.Errorf("Unable to find your organization")
	}
exit:
	return orgaID, nil
}
開發者ID:QuentinPerez,項目名稱:scaleway-cli,代碼行數:30,代碼來源:login.go

示例3: getToken

func getToken(connect api.ScalewayConnect) (string, error) {
	FakeConnection, err := api.NewScalewayAPI("", "", scwversion.UserAgent(), "", clilogger.SetupLogger)
	if err != nil {
		return "", fmt.Errorf("Unable to create a fake ScalewayAPI: %s", err)
	}
	FakeConnection.SetPassword(connect.Password)

	resp, err := FakeConnection.PostResponse(api.AccountAPI, "tokens", connect)
	if resp != nil {
		defer resp.Body.Close()
	}
	if err != nil {
		return "", fmt.Errorf("unable to connect %v", err)
	}

	// Succeed POST code
	if resp.StatusCode != 201 {
		return "", fmt.Errorf("[%d] maybe your email or your password is not valid", resp.StatusCode)
	}
	var data api.ScalewayConnectResponse

	decoder := json.NewDecoder(resp.Body)
	err = decoder.Decode(&data)
	if err != nil {
		return "", err
	}
	return data.Token.ID, nil
}
開發者ID:QuentinPerez,項目名稱:scaleway-cli,代碼行數:28,代碼來源:login.go

示例4: RunLogin

// RunLogin is the handler for 'scw login'
func RunLogin(ctx CommandContext, args LoginArgs) error {
	if args.Organization == "" || args.Token == "" {
		var err error

		args.Organization, args.Token, err = connectAPI()
		if err != nil {
			return err
		}
	}

	cfg := &config.Config{
		Organization: strings.Trim(args.Organization, "\n"),
		Token:        strings.Trim(args.Token, "\n"),
	}

	apiConnection, err := api.NewScalewayAPI(cfg.Organization, cfg.Token, scwversion.UserAgent())
	if err != nil {
		return fmt.Errorf("Unable to create ScalewayAPI: %s", err)
	}
	err = apiConnection.CheckCredentials()
	if err != nil {
		return fmt.Errorf("Unable to contact ScalewayAPI: %s", err)
	}
	if !args.SkipSSHKey {
		if err := selectKey(&args); err != nil {
			logrus.Errorf("Unable to select a key: %v", err)
		} else {
			if args.SSHKey != "" {
				uploadSSHKeys(apiConnection, args.SSHKey)
			}
		}
	}
	return cfg.Save()
}
開發者ID:moul,項目名稱:scaleway-cli,代碼行數:35,代碼來源:login.go

示例5: RealAPIContext

func RealAPIContext() *CommandContext {
	config, err := config.GetConfig()
	if err != nil {
		logrus.Warnf("RealAPIContext: failed to call config.GetConfig(): %v", err)
		return nil
	}

	apiClient, err := api.NewScalewayAPI(config.ComputeAPI, config.AccountAPI, config.Organization, config.Token)
	if err != nil {
		logrus.Warnf("RealAPIContext: failed to call api.NewScalewayAPI(): %v", err)
		return nil
	}

	stdout := bytes.Buffer{}
	stderr := bytes.Buffer{}

	ctx := CommandContext{
		Streams: Streams{
			Stdin:  os.Stdin,
			Stdout: &stdout,
			Stderr: &stderr,
		},
		Env: []string{
			"HOME" + os.Getenv("HOME"),
		},
		RawArgs: []string{},
		API:     apiClient,
	}
	return &ctx
}
開發者ID:arianvp,項目名稱:scaleway-cli,代碼行數:30,代碼來源:test.go

示例6: main

func main() {
	router := gin.Default()

	router.LoadHTMLGlob("templates/*")

	router.GET("/", indexEndpoint)

	router.GET("/images", imagesEndpoint)
	router.GET("/images/:name", imageEndpoint)
	router.GET("/images/:name/dockerfile", imageDockerfileEndpoint)

	router.GET("/cache", cacheEndpoint)

	router.GET("/images/:name/new-server", newServerEndpoint)
	// router.GET("/images/:name/badge", imageBadgeEndpoint)

	Api, err := api.NewScalewayAPI("https://api.scaleway.com", "", os.Getenv("SCALEWAY_ORGANIZATION"), os.Getenv("SCALEWAY_TOKEN"))
	if err != nil {
		logrus.Fatalf("Failed to initialize Scaleway Api: %v", err)
	}

	go updateManifestCron(&cache)
	go updateScwApiImages(Api, &cache)
	// go updateScwApiBootscripts(Api, &cache)

	router.Run(":4242")
}
開發者ID:moul,項目名稱:devhub,代碼行數:27,代碼來源:main.go

示例7: RealAPIContext

// RealAPIContext returns a CommandContext with a configured API
func RealAPIContext() *CommandContext {
	if os.Getenv("TEST_WITH_REAL_API") == "0" {
		return nil
	}
	config, err := config.GetConfig()
	if err != nil {
		logrus.Warnf("RealAPIContext: failed to call config.GetConfig(): %v", err)
		return nil
	}

	apiClient, err := api.NewScalewayAPI(config.Organization, config.Token, scwversion.UserAgent(), "par1")
	if err != nil {
		logrus.Warnf("RealAPIContext: failed to call api.NewScalewayAPI(): %v", err)
		return nil
	}

	stdout := bytes.Buffer{}
	stderr := bytes.Buffer{}

	ctx := CommandContext{
		Streams: Streams{
			Stdin:  os.Stdin,
			Stdout: &stdout,
			Stderr: &stderr,
		},
		Env: []string{
			"HOME" + os.Getenv("HOME"),
		},
		RawArgs: []string{},
		API:     apiClient,
	}
	return &ctx
}
開發者ID:pulcy,項目名稱:quark,代碼行數:34,代碼來源:test.go

示例8: getScalewayAPI

// getScalewayAPI returns a ScalewayAPI using the user config file
func getScalewayAPI() (*api.ScalewayAPI, error) {
	// We already get config globally, but whis way we can get explicit error when trying to create a ScalewayAPI object
	config, err := config.GetConfig()
	if err != nil {
		return nil, err
	}
	return api.NewScalewayAPI(config.Organization, config.Token, scwversion.UserAgent())
}
開發者ID:moul,項目名稱:scaleway-cli,代碼行數:9,代碼來源:main.go

示例9: getScalewayAPI

// getScalewayAPI returns a ScalewayAPI using the user config file
func getScalewayAPI() (*api.ScalewayAPI, error) {
	// We already get config globally, but whis way we can get explicit error when trying to create a ScalewayAPI object
	config, err := config.GetConfig()
	if err != nil {
		return nil, err
	}
	return api.NewScalewayAPI(os.Getenv("scaleway_api_endpoint"), config.AccountAPI, config.Organization, config.Token)
}
開發者ID:ebfe,項目名稱:scaleway-cli,代碼行數:9,代碼來源:main.go

示例10: main

func main() {
	router := gin.Default()

	router.StaticFile("/", "./static/index.html")
	router.Static("/static", "./static")
	router.Static("/bower_components", "./bower_components")
	// FIXME: favicon

	router.GET("/api/images", imagesEndpoint)
	router.GET("/api/images/:name", imageEndpoint)
	router.GET("/api/images/:name/dockerfile", imageDockerfileEndpoint)
	router.GET("/api/images/:name/makefile", imageMakefileEndpoint)

	router.GET("/api/bootscripts", bootscriptsEndpoint)
	router.GET("/api/bootscripts/:id", bootscriptEndpoint)

	router.GET("/api/cache", cacheEndpoint)

	router.GET("/images/:name/new-server", newServerEndpoint)

	router.GET("/badges/images/:name/scw-build.svg", badgeImageScwBuildEndpoint)
	// router.GET("/images/:name/badge", imageBadgeEndpoint)

	Api, err := api.NewScalewayAPI(os.Getenv("SCALEWAY_ORGANIZATION"), os.Getenv("SCALEWAY_TOKEN"), "devhub")
	if err != nil {
		logrus.Fatalf("Failed to initialize Scaleway Api: %v", err)
	}

	if os.Getenv("GITHUB_TOKEN") != "" {
		ts := oauth2.StaticTokenSource(
			&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
		)
		tc := oauth2.NewClient(oauth2.NoContext, ts)
		gh := github.NewClient(tc)
		go devhub.UpdateGitHub(gh, cache)
	} else {
		logrus.Errorf("GITHUB_TOKEN empty")
	}

	go devhub.UpdateManifestCron(cache)
	go devhub.UpdateScwApiImages(Api, cache)
	go devhub.UpdateScwApiBootscripts(Api, cache)
	go devhub.UpdateDockerHub(cache)

	port := os.Getenv("PORT")
	if port == "" {
		port = "8000"
	}
	router.Run(fmt.Sprintf(":%s", port))
}
開發者ID:scaleway,項目名稱:devhub,代碼行數:50,代碼來源:main.go

示例11: Client

// Client configures and returns a fully initialized Scaleway client
func (c *Config) Client() (*Client, error) {
	api, err := api.NewScalewayAPI(
		c.Organization,
		c.APIKey,
		scwversion.UserAgent(),
		func(s *api.ScalewayAPI) {
			s.Logger = newTerraformLogger()
		},
	)
	if err != nil {
		return nil, err
	}
	return &Client{api}, nil
}
開發者ID:Originate,項目名稱:terraform,代碼行數:15,代碼來源:config.go

示例12: testCommandContext

func testCommandContext() CommandContext {
	apiClient, err := api.NewScalewayAPI("https://example.org/", "https://example.org/", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
	if err != nil {
		panic(err)
	}

	ctx := CommandContext{
		Streams: Streams{
			Stdin:  os.Stdin,
			Stdout: os.Stdout,
			Stderr: os.Stderr,
		},
		Env: []string{
			"HOME" + os.Getenv("HOME"),
		},
		RawArgs: []string{},
		API:     apiClient,
	}
	return ctx
}
開發者ID:arianvp,項目名稱:scaleway-cli,代碼行數:20,代碼來源:command_test.go

示例13: testCommandContext

func testCommandContext() CommandContext {
	apiClient, err := api.NewScalewayAPI("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", scwversion.UserAgent(), "")
	if err != nil {
		panic(err)
	}

	ctx := CommandContext{
		Streams: Streams{
			Stdin:  os.Stdin,
			Stdout: os.Stdout,
			Stderr: os.Stderr,
		},
		Env: []string{
			"HOME" + os.Getenv("HOME"),
		},
		RawArgs: []string{},
		API:     apiClient,
	}
	return ctx
}
開發者ID:QuentinPerez,項目名稱:scaleway-cli,代碼行數:20,代碼來源:command_test.go

示例14: NewProvider

// NewProvider creates a new Scaleway provider implementation
func NewProvider(logger *logging.Logger, config ScalewayProviderConfig) (providers.CloudProvider, error) {
	if config.Organization == "" {
		return nil, maskAny(fmt.Errorf("Organization not set"))
	}
	if config.Token == "" {
		return nil, maskAny(fmt.Errorf("Token not set"))
	}
	if config.Region == "" {
		return nil, maskAny(fmt.Errorf("Region not set"))
	}
	client, err := api.NewScalewayAPI(config.Organization, config.Token, "quark", config.Region)
	if err != nil {
		return nil, maskAny(err)
	}
	client.Logger = NewScalewayLogger(logger)
	return &scalewayProvider{
		ScalewayProviderConfig: config,
		Logger:                 logger,
		client:                 client,
	}, nil
}
開發者ID:pulcy,項目名稱:quark,代碼行數:22,代碼來源:provider.go

示例15: ExampleCommandContext

func ExampleCommandContext() {
	apiClient, err := api.NewScalewayAPI("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", scwversion.UserAgent(), "")
	if err != nil {
		panic(err)
	}

	ctx := CommandContext{
		Streams: Streams{
			Stdin:  os.Stdin,
			Stdout: os.Stdout,
			Stderr: os.Stderr,
		},
		Env: []string{
			"HOME" + os.Getenv("HOME"),
		},
		RawArgs: []string{},
		API:     apiClient,
	}

	// Do stuff
	fmt.Println(ctx)
}
開發者ID:QuentinPerez,項目名稱:scaleway-cli,代碼行數:22,代碼來源:command_test.go


注:本文中的github.com/scaleway/scaleway-cli/pkg/api.NewScalewayAPI函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。