Commit 4d5d5138 authored by Yannik Dällenbach's avatar Yannik Dällenbach 🤼 Committed by Yannik Dällenbach
Browse files

Configure API client on `Init`

parent 005bc200
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -3,11 +3,13 @@ module cloudscale-fleeting-plugin
go 1.24.3

require (
	github.com/cloudscale-ch/cloudscale-go-sdk/v6 v6.0.0
	github.com/hashicorp/go-hclog v1.6.3
	gitlab.com/gitlab-org/fleeting/fleeting v0.0.0-20250425145049-7f673e7c5598
)

require (
	github.com/cenkalti/backoff/v5 v5.0.2 // indirect
	github.com/fatih/color v1.17.0 // indirect
	github.com/golang/protobuf v1.5.4 // indirect
	github.com/hashicorp/go-plugin v1.6.1 // indirect
+4 −0
+22 −3
Original line number Diff line number Diff line
@@ -2,24 +2,43 @@ package main

import (
	"context"
	"fmt"
	"math"
	"net/http"

	"github.com/cloudscale-ch/cloudscale-go-sdk/v6"
	hclog "github.com/hashicorp/go-hclog"
	"gitlab.com/gitlab-org/fleeting/fleeting/plugin"
	"gitlab.com/gitlab-org/fleeting/fleeting/provider"
)

type instanceGroup struct{}
type instanceGroup struct {
	ApiToken string `json:"api_token"` // Holds the cloudscale API token configured in `plugin_config`.
	client   *cloudscale.Client

	settings provider.Settings
}

// Check if interface `InstanceGroup` is implemented.
var _ provider.InstanceGroup = (*instanceGroup)(nil)

// Init implements provider.InstanceGroup.
func (g *instanceGroup) Init(ctx context.Context, logger hclog.Logger, settings provider.Settings) (provider.ProviderInfo, error) {
	return provider.ProviderInfo{
	info := provider.ProviderInfo{
		ID:      "cloudscale",
		MaxSize: math.MaxInt,
	}, nil
	}

	g.settings = settings

	g.client = cloudscale.NewClient(http.DefaultClient)
	g.client.AuthToken = g.ApiToken

	if _, err := g.client.Servers.List(ctx); err != nil {
		return info, fmt.Errorf("failed to create cloudscale API client: %w", err)
	}

	return info, nil
}

// Increase implements provider.InstanceGroup.