-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GCLOUD2-16682 Added metadata item support for instances (#143)
- Loading branch information
1 parent
9e3715e
commit 1852739
Showing
6 changed files
with
153 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package testing |
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,22 @@ | ||
package testing | ||
|
||
import ( | ||
"github.com/G-Core/gcorelabscloud-go/gcore/utils/metadata" | ||
) | ||
|
||
const MetadataResponse = ` | ||
{ | ||
"key": "db.name", | ||
"value": "pg", | ||
"read_only": false | ||
} | ||
` | ||
|
||
var ( | ||
instanceID = "ad1bb86e-2f83-4e0f-87c0-e1fd777d6352" | ||
Metadata = metadata.Metadata{ | ||
Key: "db.name", | ||
Value: "pg", | ||
ReadOnly: false, | ||
} | ||
) |
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,17 @@ | ||
package testing | ||
|
||
import ( | ||
"testing" | ||
|
||
instancesV2 "github.com/G-Core/gcorelabscloud-go/gcore/instance/v2/instances" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMetadataItemEmptyParam(t *testing.T) { | ||
opts := instancesV2.MetadataItemOpts{ | ||
Key: "", | ||
} | ||
_, err := opts.ToMetadataItemQuery() | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "Key is a required field") | ||
} |
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,57 @@ | ||
package testing | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
instancesV2 "github.com/G-Core/gcorelabscloud-go/gcore/instance/v2/instances" | ||
th "github.com/G-Core/gcorelabscloud-go/testhelper" | ||
fake "github.com/G-Core/gcorelabscloud-go/testhelper/client" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func prepareMetadataItemTestURL(id string) string { | ||
return fmt.Sprintf("/%s/instances/%d/%d/%s/%s", "v2", fake.ProjectID, fake.RegionID, id, "metadata_item") | ||
} | ||
|
||
func TestMetadataItemGet(t *testing.T) { | ||
th.SetupHTTP() | ||
defer th.TeardownHTTP() | ||
|
||
th.Mux.HandleFunc(prepareMetadataItemTestURL(instanceID), func(w http.ResponseWriter, r *http.Request) { | ||
th.TestMethod(t, r, "GET") | ||
th.TestHeader(t, r, "Authorization", fmt.Sprintf("Bearer %s", fake.AccessToken)) | ||
|
||
w.Header().Add("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_, err := fmt.Fprint(w, MetadataResponse) | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
}) | ||
|
||
client := fake.ServiceTokenClient("instances", "v2") | ||
|
||
actual, err := instancesV2.MetadataItemGet(client, instanceID, instancesV2.MetadataItemOpts{Key: Metadata.Key}).Extract() | ||
require.NoError(t, err) | ||
require.Equal(t, &Metadata, actual) | ||
} | ||
|
||
func TestMetadataItemDelete(t *testing.T) { | ||
th.SetupHTTP() | ||
defer th.TeardownHTTP() | ||
|
||
th.Mux.HandleFunc(prepareMetadataItemTestURL(instanceID), func(w http.ResponseWriter, r *http.Request) { | ||
th.TestMethod(t, r, "DELETE") | ||
th.TestHeader(t, r, "Authorization", fmt.Sprintf("Bearer %s", fake.AccessToken)) | ||
th.TestHeader(t, r, "Accept", "application/json") | ||
w.Header().Add("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
|
||
client := fake.ServiceTokenClient("instances", "v2") | ||
err := instancesV2.MetadataItemDelete(client, instanceID, instancesV2.MetadataItemOpts{Key: Metadata.Key}).ExtractErr() | ||
require.NoError(t, err) | ||
} |
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