Exemple #1
0
// VerifyCredential verifies whether the users DO credentials (access token) is
// valid or not
func (s *Stack) VerifyCredential(c *stack.Credential) error {
	cred := c.Credential.(*Credential)

	if err := cred.Valid(); err != nil {
		return err
	}

	oauthClient := oauth2.NewClient(
		oauth2.NoContext,
		oauth2.StaticTokenSource(&oauth2.Token{AccessToken: cred.AccessToken}),
	)

	client := godo.NewClient(oauthClient)

	// let's retrieve our Account information. If it's successful, we're good
	// to go
	_, _, err := client.Account.Get()
	if err != nil {
		return &stack.Error{
			Err: err,
		}
	}

	return nil
}
Exemple #2
0
func GetClient() *godo.Client {
	token := &oauth2.Token{AccessToken: token}
	t := oauth2.StaticTokenSource(token)

	oauthClient := oauth2.NewClient(oauth2.NoContext, t)
	return godo.NewClient(oauthClient)
}
Exemple #3
0
func (d *Driver) getClient() *godo.Client {
	token := &oauth2.Token{AccessToken: d.AccessToken}
	tokenSource := oauth2.StaticTokenSource(token)
	client := oauth2.NewClient(oauth2.NoContext, tokenSource)

	return godo.NewClient(client)
}
Exemple #4
0
// GetClient returns a godo client.
func GetClient(token string) *godo.Client {
	tokenSource := &TokenSource{AccessToken: token}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)
	client.BaseURL = GodoBase
	return client
}
Exemple #5
0
// New returns a new instance of DoImages
func New(conf *DoConfig) (*DoImages, error) {
	if conf.Token == "" {
		return nil, errors.New("Access Token is not set. Please check your configuration.")
	}

	// increase the timeout
	timeout := time.Second * 30
	client := &http.Client{
		Transport: &http.Transport{TLSHandshakeTimeout: timeout},
		Timeout:   timeout,
	}

	// we need to pass the client with the context itself
	ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, client)

	oauthClient := oauth2.NewClient(ctx, &tokenSource{
		AccessToken: conf.Token,
	})

	godoClient := godo.NewClient(oauthClient)

	return &DoImages{
		client: godoClient,
		images: make([]godo.Image, 0),
	}, nil
}
Exemple #6
0
func (d *Driver) getClient() *godo.Client {
	t := &oauth.Transport{
		Token: &oauth.Token{AccessToken: d.AccessToken},
	}

	return godo.NewClient(t.Client())
}
Exemple #7
0
func clientFromToken(pat string) *godo.Client {
	tokenSrc := &tokenSource{
		AccessToken: pat,
	}

	return godo.NewClient(oauth2.NewClient(oauth2.NoContext, tokenSrc))
}
Exemple #8
0
func loadDoClient() {
	tokenSource := &TokenSource{
		AccessToken: globalConfig.APIToken,
	}

	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	doClient = godo.NewClient(oauthClient)
}
Exemple #9
0
func (dm *DoDropletMarshaller) GetClientHandle(token string) *godo.Client {
	tokenSource := &TokenSource{
		AccessToken: token,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)
	return client
}
Exemple #10
0
func getClinet(accessToken string) *godo.Client {

	t := &oauth.Transport{
		Token: &oauth.Token{AccessToken: accessToken},
	}
	return godo.NewClient(t.Client())

}
Exemple #11
0
func NewDOClient(token string) *godo.Client {
	tokenSource := &TokenSource{
		AccessToken: token,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)
	return client
}
Exemple #12
0
// NewClient creates creates a godo.Client givent a token.
func (cs *LiveConfig) NewClient(token string) *godo.Client {
	tokenSource := &TokenSource{
		AccessToken: token,
	}

	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	return godo.NewClient(oauthClient)
}
Exemple #13
0
func MakeDigitalOcean(token string) *DigitalOcean {
	do := new(DigitalOcean)
	tokenSource := &TokenSource{
		AccessToken: token,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	do.client = godo.NewClient(oauthClient)
	return do
}
func getClient(token string) *godo.Client {
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: token},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)

	client := godo.NewClient(tc)

	return client
}
Exemple #15
0
func withClient(handler http.Handler, fn func(client *godo.Client) error) error {
	server := httptest.NewServer(handler)
	defer server.Close()

	client := godo.NewClient(nil)
	url, _ := url.Parse(server.URL)
	client.BaseURL = url

	return fn(client)
}
Exemple #16
0
func MakeDigitalOcean(region string, token string) *DigitalOcean {
	this := new(DigitalOcean)
	this.region = region
	tokenSource := &TokenSource{
		AccessToken: token,
	}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	this.client = godo.NewClient(oauthClient)
	return this
}
Exemple #17
0
func NewDropletHandler(settings Settings) *DropletHandler {
	retval := new(DropletHandler)
	retval.settings = settings

	// setup DO client
	tokenSource := &tokenSource{settings.ApiToken}
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	retval.client = godo.NewClient(oauthClient)

	return retval
}
Exemple #18
0
// Client() returns a new client for accessing digital ocean.
func (c *Config) Client() (*godo.Client, error) {
	tokenSrc := oauth2.StaticTokenSource(&oauth2.Token{
		AccessToken: c.Token,
	})

	client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, tokenSrc))

	log.Printf("[INFO] DigitalOcean Client configured for URL: %s", client.BaseURL.String())

	return client, nil
}
Exemple #19
0
func (p *digitalOceanProvider) Setup() error {
	readEnv()
	token := envSet.Var("DO_TOKEN").Value.Get().(string)
	if token == "" {
		return fmt.Errorf("DO_TOKEN required for Digital Ocean provider")
	}
	tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	p.client = godo.NewClient(oauthClient)
	_, _, err := p.client.Account.Get()
	return err
}
Exemple #20
0
// Setup a new client with an access_token.
func (p *DOProvider) Setup() error {
	token := env.String("do_access_token", "", "digitalocean PAT token")
	if token == "" {
		return errors.New("access key required")
	}

	t := &oauth.Transport{
		Token: &oauth.Token{AccessToken: token},
	}

	p.client = godo.NewClient(t.Client())

	return nil
}
Exemple #21
0
func dyndnsUpdate(token, record, kind string, ip net.IP) error {
	host, domain, err := parseFqdn(record)
	if err != nil {
		return err
	}

	oauthClient := oauth2.NewClient(oauth2.NoContext, NewTokenString(token))
	doClient := godo.NewClient(oauthClient)
	listOpts := &godo.ListOptions{}

	records, _, err := doClient.Domains.Records(domain, listOpts)
	if err != nil {
		return err
	}

	var id int
	for _, r := range records {
		if r.Type == kind && r.Name == host {
			if r.Data == ip.String() {
				fmt.Printf("%s %s up to date.\n", record, kind)
				return nil
			}
			id = r.ID
			break
		}
	}

	edit := &godo.DomainRecordEditRequest{
		Type: kind,
		Name: host,
		Data: ip.String(),
	}

	if id == 0 {
		// create
		_, _, err := doClient.Domains.CreateRecord(domain, edit)
		if err != nil {
			return err
		}
		fmt.Printf("%s %s created.\n", record, kind)
	} else {
		_, _, err := doClient.Domains.EditRecord(domain, id, edit)
		if err != nil {
			return err
		}
		fmt.Printf("%s %s updated.\n", record, kind)
	}

	return nil
}
Exemple #22
0
// New creates a Client.
func New(token string) *Client {
	ts := &tokenSource{
		AccessToken: token,
	}

	oauthClient := oauth2.NewClient(oauth2.NoContext, ts)
	godoClient := godo.NewClient(oauthClient)

	c := &Client{Client: godoClient}
	tagServ := &tagsService{client: c}

	c.Tags = tagServ

	return c
}
Exemple #23
0
// newMachine returns a provider.Machine that can be used to manager DO
// droplets.
func newMachine(bm *provider.BaseMachine) (provider.Machine, error) {
	cred, ok := bm.Credential.(*Credential)
	if !ok {
		return nil, errors.New("not a valid DigitalOcean credential")
	}

	oauthClient := oauth2.NewClient(
		oauth2.NoContext,
		oauth2.StaticTokenSource(&oauth2.Token{AccessToken: cred.AccessToken}),
	)

	return &Machine{
		client:      godo.NewClient(oauthClient),
		BaseMachine: bm,
	}, nil
}
Exemple #24
0
func main() {
	accessToken := os.Getenv(`DO_KEY`)
	if accessToken == `` {
		log.Fatal(`Usage: DO_KEY environment variable must be set.`)
	}

	// setup dependencies
	oauthClient := oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken}))
	apiClient := godo.NewClient(oauthClient)
	metaClient := metadata.NewClient()
	ipt, err := iptables.New()
	failIfErr(err)

	// collect needed metadata from metadata service
	region, err := metaClient.Region()
	failIfErr(err)
	mData, err := metaClient.Metadata()
	failIfErr(err)

	// collect list of all droplets
	drops, err := DropletList(apiClient.Droplets)
	failIfErr(err)

	allowed, ok := SortDroplets(drops)[region]
	if !ok {
		log.Fatalf(`No droplets listed in region [%s]`, region)
	}

	// collect local network interface information
	local, err := LocalAddress(mData)
	failIfErr(err)
	ifaces, err := net.Interfaces()
	failIfErr(err)
	iface, err := PrivateInterface(ifaces, local)
	failIfErr(err)

	// setup dolan-peers chain for local interface
	err = Setup(ipt, iface)
	failIfErr(err)

	// update dolan-peers
	err = UpdatePeers(ipt, allowed)
	failIfErr(err)
	log.Printf(`Added %d peers to dolan-peers`, len(allowed))
}
Exemple #25
0
func ExampleWaitForActive() {
	// build client
	pat := "mytoken"
	token := &oauth2.Token{AccessToken: pat}
	t := oauth2.StaticTokenSource(token)

	oauthClient := oauth2.NewClient(oauth2.NoContext, t)
	client := godo.NewClient(oauthClient)

	// create your droplet and retrieve the create action uri
	uri := "https://api.digitalocean.com/v2/actions/xxxxxxxx"

	// block until until the action is complete
	err := WaitForActive(client, uri)
	if err != nil {
		panic(err)
	}
}
Exemple #26
0
func ExampleWaitForActive() {
	// build client
	pat := "mytoken"
	t := &oauth.Transport{
		Token: &oauth.Token{AccessToken: pat},
	}

	client := godo.NewClient(t.Client())

	// create your droplet and retrieve the create action uri
	uri := "https://api.digitalocean.com/v2/actions/xxxxxxxx"

	// block until until the action is complete
	err := WaitForActive(client, uri)
	if err != nil {
		panic(err)
	}
}
Exemple #27
0
func (i *digitalOceanIaas) Auth() error {
	u, _ := i.base.GetConfigString("url")
	token, err := i.base.GetConfigString("token")
	if err != nil {
		return err
	}
	client := *net.Dial5Full300Client
	client.Transport = &oauth2.Transport{
		Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}),
		Base:   net.Dial5Full300Client.Transport,
	}
	i.client = godo.NewClient(&client)
	if u != "" {
		i.client.BaseURL, err = url.Parse(u)
		if err != nil {
			return err
		}
	}
	return nil
}
Exemple #28
0
func Init(configPath string) (*context, error) {
	rand.Seed(time.Now().UnixNano())
	data, err := ioutil.ReadFile(configPath)
	if err != nil {
		return nil, err
	}
	config := new(configuration)
	config.DropletSSHKeyID = -1 // Default Value
	err = yaml.Unmarshal(data, config)
	if err != nil {
		return nil, err
	}

	// Starting mysql connection
	databaseHost := fmt.Sprintf("%v:%v@tcp(%v)/%v?charset=utf8&parseTime=True&loc=Local", config.Database["user"], config.Database["password"], config.Database["host"], config.Database["dbname"])
	tmp2, err := gorm.Open("mysql", databaseHost)
	if err != nil {
		return nil, err
	}
	tmp2.AutoMigrate(&Instance{})

	// Starting redis connection
	tmp3, err := redis.Dial("tcp", config.RedisAddress)
	if err != nil {
		return nil, err
	}
	tmp3.Do("AUTH", config.RedisPassword)

	// Starting digital ocean client
	oauthClient := oauth2.NewClient(oauth2.NoContext, &TokenSource{AccessToken: config.DigitalOceanToken})
	tmp4 := godo.NewClient(oauthClient)

	ctx := context{
		config:       *config,
		db:           tmp2,
		redis:        tmp3,
		digitalocean: *tmp4,
	}

	return &ctx, nil
}
Exemple #29
0
// NewProvisioner creates a new Provisioner with the specified DigitalOcean token.
func NewProvisioner(token string) (Provisioner, error) {
	if len(token) == 0 {
		return nil, ErrNoToken
	}

	tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
	client := godo.NewClient(oauthClient)
	if !checkToken(client) {
		return nil, ErrTokenInvalid
	}

	result := &provisioner{
		client:    client,
		endpoints: make(map[string]*Endpoint),
		signal:    make(chan struct{}),
	}
	go result.restoreEndpoints()

	return result, nil
}
Exemple #30
-1
func (pc *NodeController) getNodeAddDOAction(c *gin.Context) {

	a, err := models.AccessMapper.FetchOne("do")
	if err != nil {
		panic(err)
	}

	if a == nil {

		c.HTML(http.StatusOK, "node_add_do.html", map[string]interface{}{})
		return
	}

	oauthClient := oauth2.NewClient(oauth2.NoContext, &DOTokenSource{
		AccessToken: a.AccessKey,
	})
	client := godo.NewClient(oauthClient)

	regions, _, err := client.Regions.List(&godo.ListOptions{})
	if err != nil {
		panic(err)
	}

	sizes, _, err := client.Sizes.List(&godo.ListOptions{})
	if err != nil {
		panic(err)
	}

	c.HTML(http.StatusOK, "node_add_do.html", map[string]interface{}{
		"AccessKey": true,
		"DORegions": regions,
		"DOSizes":   sizes,
	})
}