-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Marketplace App Variables (#291)
* Add marketplace service, list variables endpoint, and test * Support marketplace app variables on instance/BM create
- Loading branch information
1 parent
ec9fca1
commit f40104b
Showing
5 changed files
with
125 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package govultr | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
const marketplacePath = "/v2/marketplace" | ||
|
||
// MarketplaceService is the interface to interact with the Marketplace endpoints on the Vultr API | ||
// Link: https://www.vultr.com/api/#tag/marketplace | ||
type MarketplaceService interface { | ||
ListAppVariables(ctx context.Context, imageID string) ([]MarketplaceAppVariable, *http.Response, error) | ||
} | ||
|
||
// MarketplaceServiceHandler handles interaction with the server methods for the Vultr API | ||
type MarketplaceServiceHandler struct { | ||
client *Client | ||
} | ||
|
||
// MarketplaceAppVariable represents a user-supplied variable for a Marketplace app | ||
type MarketplaceAppVariable struct { | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
Required *bool `json:"required"` | ||
} | ||
|
||
// marketplaceAppVariablesBase holds the API response for retrieving a list of user-supplied variables for a Marketplace app | ||
type marketplaceAppVariablesBase struct { | ||
MarketplaceAppVariables []MarketplaceAppVariable `json:"variables"` | ||
} | ||
|
||
// ListAppVariables retrieves all user-supplied variables for a Marketplace app | ||
func (d *MarketplaceServiceHandler) ListAppVariables(ctx context.Context, imageID string) ([]MarketplaceAppVariable, *http.Response, error) { //nolint:lll | ||
uri := fmt.Sprintf("%s/apps/%s/variables", marketplacePath, imageID) | ||
|
||
req, err := d.client.NewRequest(ctx, http.MethodGet, uri, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
marketplaceAppVariables := new(marketplaceAppVariablesBase) | ||
resp, err := d.client.DoWithContext(ctx, req, marketplaceAppVariables) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return marketplaceAppVariables.MarketplaceAppVariables, resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package govultr | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestMarketplaceServiceHandler_ListAppVariables(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc(fmt.Sprintf("/v2/marketplace/apps/%s/variables", "testimage"), func(writer http.ResponseWriter, request *http.Request) { | ||
response := `{ | ||
"variables": [ | ||
{ | ||
"name": "some_required_variable", | ||
"description": "This is an example of a required user-supplied variable for this Marketplace app.", | ||
"required": true | ||
}, | ||
{ | ||
"name": "some_optional_variable", | ||
"description": "This is an example of an optional user-supplied variable for this Marketplace app.", | ||
"required": false | ||
} | ||
] | ||
}` | ||
fmt.Fprint(writer, response) | ||
}) | ||
|
||
variables, _, err := client.Marketplace.ListAppVariables(ctx, "testimage") | ||
if err != nil { | ||
t.Errorf("Marketplace.ListAppVariables returned %+v", err) | ||
} | ||
|
||
expected := []MarketplaceAppVariable{ | ||
{ | ||
Name: "some_required_variable", | ||
Description: "This is an example of a required user-supplied variable for this Marketplace app.", | ||
Required: BoolToBoolPtr(true), | ||
}, | ||
{ | ||
Name: "some_optional_variable", | ||
Description: "This is an example of an optional user-supplied variable for this Marketplace app.", | ||
Required: BoolToBoolPtr(false), | ||
}, | ||
} | ||
|
||
if !reflect.DeepEqual(variables, expected) { | ||
t.Errorf("Marketplace.ListAppVariables returned %+v, expected %+v", variables, expected) | ||
} | ||
} |