Skip to content

Commit a5b4efb

Browse files
author
Kamal Nasser
committed
apps: add ListBuildpacks, UpgradeBuildpack
1 parent d3a2cbf commit a5b4efb

File tree

5 files changed

+111
-72
lines changed

5 files changed

+111
-72
lines changed

apps.gen.go

-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps.go

+47
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type AppsService interface {
5151
UpdateAlertDestinations(ctx context.Context, appID, alertID string, update *AlertDestinationUpdateRequest) (*AppAlert, *Response, error)
5252

5353
Detect(ctx context.Context, detect *DetectRequest) (*DetectResponse, *Response, error)
54+
55+
ListBuildpacks(ctx context.Context) ([]*Buildpack, *Response, error)
56+
UpgradeBuildpack(ctx context.Context, appID string, opts UpgradeBuildpackOptions) (*UpgradeBuildpackResponse, *Response, error)
5457
}
5558

5659
// AppLogs represent app logs.
@@ -75,6 +78,16 @@ type AlertDestinationUpdateRequest struct {
7578
SlackWebhooks []*AppAlertSlackWebhook `json:"slack_webhooks"`
7679
}
7780

81+
// UpgradeBuildpackOptions struct for UpgradeBuildpackOptions
82+
type UpgradeBuildpackOptions struct {
83+
// The ID of the buildpack to upgrade.
84+
BuildpackID string `json:"buildpack_id,omitempty"`
85+
// The Major Version to upgrade the buildpack to. If omitted, the latest available major version will be used.
86+
MajorVersion int32 `json:"major_version,omitempty"`
87+
// Whether or not to trigger a deployment for the app after upgrading the buildpack.
88+
TriggerDeployment bool `json:"trigger_deployment,omitempty"`
89+
}
90+
7891
type appRoot struct {
7992
App *App `json:"app"`
8093
}
@@ -123,6 +136,10 @@ type appAlertRoot struct {
123136
Alert *AppAlert `json:"alert"`
124137
}
125138

139+
type buildpacksRoot struct {
140+
Buildpacks []*Buildpack `json:"buildpacks,omitempty"`
141+
}
142+
126143
// AppsServiceOp handles communication with Apps methods of the DigitalOcean API.
127144
type AppsServiceOp struct {
128145
client *Client
@@ -444,6 +461,36 @@ func (s *AppsServiceOp) Detect(ctx context.Context, detect *DetectRequest) (*Det
444461
return res, resp, nil
445462
}
446463

464+
// ListBuildpacks lists the available buildpacks on App Platform.
465+
func (s *AppsServiceOp) ListBuildpacks(ctx context.Context) ([]*Buildpack, *Response, error) {
466+
path := fmt.Sprintf("%s/buildpacks", appsBasePath)
467+
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
468+
if err != nil {
469+
return nil, nil, err
470+
}
471+
root := new(buildpacksRoot)
472+
resp, err := s.client.Do(ctx, req, root)
473+
if err != nil {
474+
return nil, resp, err
475+
}
476+
return root.Buildpacks, resp, nil
477+
}
478+
479+
// UpgradeBuildpack upgrades a buildpack for an app.
480+
func (s *AppsServiceOp) UpgradeBuildpack(ctx context.Context, appID string, opts UpgradeBuildpackOptions) (*UpgradeBuildpackResponse, *Response, error) {
481+
path := fmt.Sprintf("%s/%s/upgrade_buildpack", appsBasePath, appID)
482+
req, err := s.client.NewRequest(ctx, http.MethodPost, path, opts)
483+
if err != nil {
484+
return nil, nil, err
485+
}
486+
root := new(UpgradeBuildpackResponse)
487+
resp, err := s.client.Do(ctx, req, root)
488+
if err != nil {
489+
return nil, resp, err
490+
}
491+
return root, resp, nil
492+
}
493+
447494
// AppComponentType is an app component type.
448495
type AppComponentType string
449496

apps_accessors.go

-32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps_accessors_test.go

-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,21 @@ var (
210210
},
211211
},
212212
}
213+
214+
testBuildpacks = []*Buildpack{
215+
{
216+
ID: "digitalocean/node",
217+
Name: "Node.js",
218+
Version: "1.2.3",
219+
MajorVersion: 1,
220+
},
221+
{
222+
ID: "digitalocean/php",
223+
Name: "PHP",
224+
Version: "0.3.5",
225+
MajorVersion: 0,
226+
},
227+
}
213228
)
214229

215230
func TestApps_CreateApp(t *testing.T) {
@@ -631,6 +646,55 @@ func TestApps_Detect(t *testing.T) {
631646
assert.Equal(t, component, res.Components[0])
632647
}
633648

649+
func TestApps_ListBuildpacks(t *testing.T) {
650+
setup()
651+
defer teardown()
652+
653+
ctx := context.Background()
654+
655+
mux.HandleFunc("/v2/apps/buildpacks", func(w http.ResponseWriter, r *http.Request) {
656+
testMethod(t, r, http.MethodGet)
657+
658+
json.NewEncoder(w).Encode(&buildpacksRoot{Buildpacks: testBuildpacks})
659+
})
660+
661+
bps, _, err := client.Apps.ListBuildpacks(ctx)
662+
require.NoError(t, err)
663+
assert.Equal(t, testBuildpacks, bps)
664+
}
665+
666+
func TestApps_UpgradeBuildpack(t *testing.T) {
667+
setup()
668+
defer teardown()
669+
670+
ctx := context.Background()
671+
672+
response := &UpgradeBuildpackResponse{
673+
AffectedComponents: []string{"api", "frontend"},
674+
Deployment: &testDeployment,
675+
}
676+
opts := UpgradeBuildpackOptions{
677+
BuildpackID: "digitalocean/node",
678+
MajorVersion: 3,
679+
TriggerDeployment: true,
680+
}
681+
682+
mux.HandleFunc(fmt.Sprintf("/v2/apps/%s/upgrade_buildpack", testApp.ID), func(w http.ResponseWriter, r *http.Request) {
683+
testMethod(t, r, http.MethodPost)
684+
685+
var gotOpts UpgradeBuildpackOptions
686+
err := json.NewDecoder(r.Body).Decode(&gotOpts)
687+
require.NoError(t, err)
688+
assert.Equal(t, opts, gotOpts)
689+
690+
json.NewEncoder(w).Encode(response)
691+
})
692+
693+
gotResponse, _, err := client.Apps.UpgradeBuildpack(ctx, testApp.ID, opts)
694+
require.NoError(t, err)
695+
assert.Equal(t, response, gotResponse)
696+
}
697+
634698
func TestApps_ToURN(t *testing.T) {
635699
app := &App{
636700
ID: "deadbeef-dead-4aa5-beef-deadbeef347d",

0 commit comments

Comments
 (0)