From 2dbc769ff00c83793ce873970d88518bcd13d01e Mon Sep 17 00:00:00 2001 From: Phil Adams Date: Wed, 23 Sep 2020 17:48:14 -0500 Subject: [PATCH 1/4] fix(Global Tagging): re-gen service with latest API definition --- globaltaggingv1/global_tagging_v1.go | 429 +++++++++++++++--- .../global_tagging_v1_examples_test.go | 246 ++++++++++ .../global_tagging_v1_integration_test.go | 326 ++++++++----- globaltaggingv1/global_tagging_v1_test.go | 293 +++++++++++- 4 files changed, 1113 insertions(+), 181 deletions(-) create mode 100644 globaltaggingv1/global_tagging_v1_examples_test.go diff --git a/globaltaggingv1/global_tagging_v1.go b/globaltaggingv1/global_tagging_v1.go index 283e0fc1..ddc50e40 100644 --- a/globaltaggingv1/global_tagging_v1.go +++ b/globaltaggingv1/global_tagging_v1.go @@ -15,7 +15,7 @@ */ /* - * IBM OpenAPI SDK Code Generator Version: 99-SNAPSHOT-ef5e13c2-20200915-144510 + * IBM OpenAPI SDK Code Generator Version: 99-SNAPSHOT-9b0d887a-20200923-132457 */ @@ -33,9 +33,11 @@ import ( // GlobalTaggingV1 : Manage your tags with the Tagging API in IBM Cloud. You can attach, detach, delete a tag or list // all tags in your billing account with the Tagging API. The tag name must be unique within a billing account. You can -// create tags in two formats: `key:value` or `label`. +// create tags in two formats: `key:value` or `label`. The tagging API supports two types of tag: `user` and `service`. +// `service` tags cannot be attached to IMS resources (see `providers=ims` query parameter). `service` tags must be in +// the form `service_prefix:tag_label` where `service_prefix` identifies the Service owning the tag. // -// Version: 1.0.3 +// Version: 1.1.0 type GlobalTaggingV1 struct { Service *core.BaseService } @@ -138,27 +140,33 @@ func (globalTagging *GlobalTaggingV1) ListTags(listTagsOptions *ListTagsOptions) } builder.AddHeader("Accept", "application/json") + if listTagsOptions.AccountID != nil { + builder.AddQuery("account_id", fmt.Sprint(*listTagsOptions.AccountID)) + } + if listTagsOptions.TagType != nil { + builder.AddQuery("tag_type", fmt.Sprint(*listTagsOptions.TagType)) + } + if listTagsOptions.FullData != nil { + builder.AddQuery("full_data", fmt.Sprint(*listTagsOptions.FullData)) + } if listTagsOptions.Providers != nil { builder.AddQuery("providers", strings.Join(listTagsOptions.Providers, ",")) } if listTagsOptions.AttachedTo != nil { builder.AddQuery("attached_to", fmt.Sprint(*listTagsOptions.AttachedTo)) } - if listTagsOptions.FullData != nil { - builder.AddQuery("full_data", fmt.Sprint(*listTagsOptions.FullData)) - } if listTagsOptions.Offset != nil { builder.AddQuery("offset", fmt.Sprint(*listTagsOptions.Offset)) } if listTagsOptions.Limit != nil { builder.AddQuery("limit", fmt.Sprint(*listTagsOptions.Limit)) } - if listTagsOptions.OrderByName != nil { - builder.AddQuery("order_by_name", fmt.Sprint(*listTagsOptions.OrderByName)) - } if listTagsOptions.Timeout != nil { builder.AddQuery("timeout", fmt.Sprint(*listTagsOptions.Timeout)) } + if listTagsOptions.OrderByName != nil { + builder.AddQuery("order_by_name", fmt.Sprint(*listTagsOptions.OrderByName)) + } if listTagsOptions.AttachedOnly != nil { builder.AddQuery("attached_only", fmt.Sprint(*listTagsOptions.AttachedOnly)) } @@ -182,8 +190,8 @@ func (globalTagging *GlobalTaggingV1) ListTags(listTagsOptions *ListTagsOptions) return } -// DeleteTagAll : Delete unused tags -// Delete the tags that are not attatched to any resource. +// DeleteTagAll : Delete all unused tags +// Delete the tags that are not attached to any resource. func (globalTagging *GlobalTaggingV1) DeleteTagAll(deleteTagAllOptions *DeleteTagAllOptions) (result *DeleteTagsResult, response *core.DetailedResponse, err error) { err = core.ValidateStruct(deleteTagAllOptions, "deleteTagAllOptions") if err != nil { @@ -209,6 +217,12 @@ func (globalTagging *GlobalTaggingV1) DeleteTagAll(deleteTagAllOptions *DeleteTa if deleteTagAllOptions.Providers != nil { builder.AddQuery("providers", fmt.Sprint(*deleteTagAllOptions.Providers)) } + if deleteTagAllOptions.AccountID != nil { + builder.AddQuery("account_id", fmt.Sprint(*deleteTagAllOptions.AccountID)) + } + if deleteTagAllOptions.TagType != nil { + builder.AddQuery("tag_type", fmt.Sprint(*deleteTagAllOptions.TagType)) + } request, err := builder.Build() if err != nil { @@ -229,7 +243,7 @@ func (globalTagging *GlobalTaggingV1) DeleteTagAll(deleteTagAllOptions *DeleteTa return } -// DeleteTag : Delete a tag +// DeleteTag : Delete an unused tag // Delete an existing tag. A tag can be deleted only if it is not attached to any resource. func (globalTagging *GlobalTaggingV1) DeleteTag(deleteTagOptions *DeleteTagOptions) (result *DeleteTagResults, response *core.DetailedResponse, err error) { err = core.ValidateNotNil(deleteTagOptions, "deleteTagOptions cannot be nil") @@ -264,6 +278,12 @@ func (globalTagging *GlobalTaggingV1) DeleteTag(deleteTagOptions *DeleteTagOptio if deleteTagOptions.Providers != nil { builder.AddQuery("providers", strings.Join(deleteTagOptions.Providers, ",")) } + if deleteTagOptions.AccountID != nil { + builder.AddQuery("account_id", fmt.Sprint(*deleteTagOptions.AccountID)) + } + if deleteTagOptions.TagType != nil { + builder.AddQuery("tag_type", fmt.Sprint(*deleteTagOptions.TagType)) + } request, err := builder.Build() if err != nil { @@ -284,11 +304,12 @@ func (globalTagging *GlobalTaggingV1) DeleteTag(deleteTagOptions *DeleteTagOptio return } -// AttachTag : Attach one or more tags -// Attaches one or more tags to one or more resources. To attach a tag to a resource managed by the Resource Controller, -// you must be an editor on the resource. To attach a tag to a Cloud Foundry resource, you must have space developer -// role. To attach a tag to IMS resources, depending on the resource, you need either `view hardware details`, `view -// virtual server details` or `manage storage` permission. +// AttachTag : Attach tags +// Attaches one or more tags to one or more resources. To attach a `user` tag on a resource, you must have the access +// listed in the [Granting users access to tag resources](https://cloud.ibm.com/docs/account?topic=account-access) +// documentation. To attach a `service` tag, you must be an authorized service. If that is the case, then you can attach +// a `service` tag with your registered `prefix` to any resource in any account. The account ID must be set through the +// `account_id` query parameter. func (globalTagging *GlobalTaggingV1) AttachTag(attachTagOptions *AttachTagOptions) (result *TagResults, response *core.DetailedResponse, err error) { err = core.ValidateNotNil(attachTagOptions, "attachTagOptions cannot be nil") if err != nil { @@ -316,6 +337,13 @@ func (globalTagging *GlobalTaggingV1) AttachTag(attachTagOptions *AttachTagOptio builder.AddHeader("Accept", "application/json") builder.AddHeader("Content-Type", "application/json") + if attachTagOptions.AccountID != nil { + builder.AddQuery("account_id", fmt.Sprint(*attachTagOptions.AccountID)) + } + if attachTagOptions.TagType != nil { + builder.AddQuery("tag_type", fmt.Sprint(*attachTagOptions.TagType)) + } + body := make(map[string]interface{}) if attachTagOptions.Resources != nil { body["resources"] = attachTagOptions.Resources @@ -350,11 +378,12 @@ func (globalTagging *GlobalTaggingV1) AttachTag(attachTagOptions *AttachTagOptio return } -// DetachTag : Detach one or more tags -// Detach one or more tags from one or more resources. To detach a tag from a Resource Controller managed resource, you -// must be an editor on the resource. To detach a tag to a Cloud Foundry resource, you must have `space developer` role. -// To detach a tag to IMS resources, depending on the resource, you need either `view hardware details`, `view virtual -// server details` or `storage manage` permission. +// DetachTag : Detach tags +// Detaches one or more tags from one or more resources. To detach a `user` tag on a resource you must have the +// permissions listed in the [Granting users access to tag +// resources](https://cloud.ibm.com/docs/account?topic=account-access) documentation. To detach a `service` tag you must +// be an authorized Service. If that is the case, then you can detach a `service` tag with your registered `prefix` from +// any resource in any account. The account ID must be set through the `account_id` query parameter. func (globalTagging *GlobalTaggingV1) DetachTag(detachTagOptions *DetachTagOptions) (result *TagResults, response *core.DetailedResponse, err error) { err = core.ValidateNotNil(detachTagOptions, "detachTagOptions cannot be nil") if err != nil { @@ -382,6 +411,13 @@ func (globalTagging *GlobalTaggingV1) DetachTag(detachTagOptions *DetachTagOptio builder.AddHeader("Accept", "application/json") builder.AddHeader("Content-Type", "application/json") + if detachTagOptions.AccountID != nil { + builder.AddQuery("account_id", fmt.Sprint(*detachTagOptions.AccountID)) + } + if detachTagOptions.TagType != nil { + builder.AddQuery("tag_type", fmt.Sprint(*detachTagOptions.TagType)) + } + body := make(map[string]interface{}) if detachTagOptions.Resources != nil { body["resources"] = detachTagOptions.Resources @@ -416,6 +452,76 @@ func (globalTagging *GlobalTaggingV1) DetachTag(detachTagOptions *DetachTagOptio return } +// SetTags : Set tags for a resource +// Tag one resource by passing in one or more tags. Tag references are cleared out every time this operation is called. +// If your resource is already tagged you will need to pass the current tags along with any new ones. To remove all tag +// references pass an empty tag_names array. To remove one or more tags omit them from the tag_names array. You can only +// set tags to a specific subset of IMS resources and only if you have the needed permission. +func (globalTagging *GlobalTaggingV1) SetTags(setTagsOptions *SetTagsOptions) (result *SetTagsResults, response *core.DetailedResponse, err error) { + err = core.ValidateNotNil(setTagsOptions, "setTagsOptions cannot be nil") + if err != nil { + return + } + err = core.ValidateStruct(setTagsOptions, "setTagsOptions") + if err != nil { + return + } + + builder := core.NewRequestBuilder(core.POST) + _, err = builder.ResolveRequestURL(globalTagging.Service.Options.URL, `/v3/tags/set_tags`, nil) + if err != nil { + return + } + + for headerName, headerValue := range setTagsOptions.Headers { + builder.AddHeader(headerName, headerValue) + } + + sdkHeaders := common.GetSdkHeaders("global_tagging", "V1", "SetTags") + for headerName, headerValue := range sdkHeaders { + builder.AddHeader(headerName, headerValue) + } + builder.AddHeader("Accept", "application/json") + builder.AddHeader("Content-Type", "application/json") + if setTagsOptions.XGhost != nil { + builder.AddHeader("X-Ghost", fmt.Sprint(*setTagsOptions.XGhost)) + } + + if setTagsOptions.AccountID != nil { + builder.AddQuery("account_id", fmt.Sprint(*setTagsOptions.AccountID)) + } + + body := make(map[string]interface{}) + if setTagsOptions.TagNames != nil { + body["tag_names"] = setTagsOptions.TagNames + } + if setTagsOptions.Resources != nil { + body["resources"] = setTagsOptions.Resources + } + _, err = builder.SetBodyContentJSON(body) + if err != nil { + return + } + + request, err := builder.Build() + if err != nil { + return + } + + var rawResponse map[string]json.RawMessage + response, err = globalTagging.Service.Request(request, &rawResponse) + if err != nil { + return + } + err = core.UnmarshalModel(rawResponse, "", &result, UnmarshalSetTagsResults) + if err != nil { + return + } + response.Result = result + + return +} + // AttachTagOptions : The AttachTag options. type AttachTagOptions struct { // List of resources on which the tag or tags should be attached. @@ -427,10 +533,26 @@ type AttachTagOptions struct { // An array of tag names to attach. TagNames []string `json:"tag_names,omitempty"` + // The ID of the billing account where the resources to be tagged lives. It is a required parameter if `tag_type` is + // set to `service`. Otherwise, it is inferred from the authorization IAM token. + AccountID *string `json:"account_id,omitempty"` + + // The type of the tag. Supported values are `user` and `service`. `service` is not supported if `providers` is set to + // `ims`. + TagType *string `json:"tag_type,omitempty"` + // Allows users to set headers on API requests Headers map[string]string } +// Constants associated with the AttachTagOptions.TagType property. +// The type of the tag. Supported values are `user` and `service`. `service` is not supported if `providers` is set to +// `ims`. +const ( + AttachTagOptions_TagType_Service = "service" + AttachTagOptions_TagType_User = "user" +) + // NewAttachTagOptions : Instantiate AttachTagOptions func (*GlobalTaggingV1) NewAttachTagOptions(resources []Resource) *AttachTagOptions { return &AttachTagOptions{ @@ -456,6 +578,18 @@ func (options *AttachTagOptions) SetTagNames(tagNames []string) *AttachTagOption return options } +// SetAccountID : Allow user to set AccountID +func (options *AttachTagOptions) SetAccountID(accountID string) *AttachTagOptions { + options.AccountID = core.StringPtr(accountID) + return options +} + +// SetTagType : Allow user to set TagType +func (options *AttachTagOptions) SetTagType(tagType string) *AttachTagOptions { + options.TagType = core.StringPtr(tagType) + return options +} + // SetHeaders : Allow user to set Headers func (options *AttachTagOptions) SetHeaders(param map[string]string) *AttachTagOptions { options.Headers = param @@ -467,6 +601,14 @@ type DeleteTagAllOptions struct { // Select a provider. Supported values are `ghost` and `ims`. Providers *string `json:"providers,omitempty"` + // The ID of the billing account to delete the tags for. If it is not set, then it is taken from the authorization + // token. It is a required parameter if `tag_type` is set to `service`. + AccountID *string `json:"account_id,omitempty"` + + // The type of the tag. Supported values are `user` and `service`. `service` is not supported if the `providers` + // parameter is set to `ims`. + TagType *string `json:"tag_type,omitempty"` + // Allows users to set headers on API requests Headers map[string]string } @@ -478,6 +620,14 @@ const ( DeleteTagAllOptions_Providers_Ims = "ims" ) +// Constants associated with the DeleteTagAllOptions.TagType property. +// The type of the tag. Supported values are `user` and `service`. `service` is not supported if the `providers` +// parameter is set to `ims`. +const ( + DeleteTagAllOptions_TagType_Service = "service" + DeleteTagAllOptions_TagType_User = "user" +) + // NewDeleteTagAllOptions : Instantiate DeleteTagAllOptions func (*GlobalTaggingV1) NewDeleteTagAllOptions() *DeleteTagAllOptions { return &DeleteTagAllOptions{} @@ -489,6 +639,18 @@ func (options *DeleteTagAllOptions) SetProviders(providers string) *DeleteTagAll return options } +// SetAccountID : Allow user to set AccountID +func (options *DeleteTagAllOptions) SetAccountID(accountID string) *DeleteTagAllOptions { + options.AccountID = core.StringPtr(accountID) + return options +} + +// SetTagType : Allow user to set TagType +func (options *DeleteTagAllOptions) SetTagType(tagType string) *DeleteTagAllOptions { + options.TagType = core.StringPtr(tagType) + return options +} + // SetHeaders : Allow user to set Headers func (options *DeleteTagAllOptions) SetHeaders(param map[string]string) *DeleteTagAllOptions { options.Headers = param @@ -503,6 +665,13 @@ type DeleteTagOptions struct { // Select a provider. Supported values are `ghost` and `ims`. To delete tag both in GhoST in IMS, use `ghost,ims`. Providers []string `json:"providers,omitempty"` + // The ID of the billing account to delete the tag for. It is a required parameter if `tag_type` is set to `service`, + // otherwise it is inferred from the authorization IAM token. + AccountID *string `json:"account_id,omitempty"` + + // The type of the tag. Supported values are `user` and `service`. `service` is not supported for `providers=ims`. + TagType *string `json:"tag_type,omitempty"` + // Allows users to set headers on API requests Headers map[string]string } @@ -513,6 +682,13 @@ const ( DeleteTagOptions_Providers_Ims = "ims" ) +// Constants associated with the DeleteTagOptions.TagType property. +// The type of the tag. Supported values are `user` and `service`. `service` is not supported for `providers=ims`. +const ( + DeleteTagOptions_TagType_Service = "service" + DeleteTagOptions_TagType_User = "user" +) + // NewDeleteTagOptions : Instantiate DeleteTagOptions func (*GlobalTaggingV1) NewDeleteTagOptions(tagName string) *DeleteTagOptions { return &DeleteTagOptions{ @@ -532,6 +708,18 @@ func (options *DeleteTagOptions) SetProviders(providers []string) *DeleteTagOpti return options } +// SetAccountID : Allow user to set AccountID +func (options *DeleteTagOptions) SetAccountID(accountID string) *DeleteTagOptions { + options.AccountID = core.StringPtr(accountID) + return options +} + +// SetTagType : Allow user to set TagType +func (options *DeleteTagOptions) SetTagType(tagType string) *DeleteTagOptions { + options.TagType = core.StringPtr(tagType) + return options +} + // SetHeaders : Allow user to set Headers func (options *DeleteTagOptions) SetHeaders(param map[string]string) *DeleteTagOptions { options.Headers = param @@ -638,14 +826,15 @@ func UnmarshalDeleteTagResultsItem(m map[string]json.RawMessage, result interfac return } -// DeleteTagsResult : The results of a deleting unattatched tags. +// DeleteTagsResult : Results of a deleting unattatched tags. type DeleteTagsResult struct { - // The number of tags deleted in the account. + // The number of tags that have been deleted. TotalCount *int64 `json:"total_count,omitempty"` - // An indicator that is set to true if there was an error deleting some of the tags. + // It is set to true if there is at least one tag operation in error. Errors *bool `json:"errors,omitempty"` + // The list of tag operation results. Items []DeleteTagsResultItem `json:"items,omitempty"` } @@ -669,12 +858,12 @@ func UnmarshalDeleteTagsResult(m map[string]json.RawMessage, result interface{}) return } -// DeleteTagsResultItem : Result of deleting one unattached tag. +// DeleteTagsResultItem : Result of a delete_tags request. type DeleteTagsResultItem struct { - // The name of the tag that was deleted. + // The name of the deleted tag. TagName *string `json:"tag_name,omitempty"` - // An indicator that is set to true if there was an error deleting the tag. + // true if the tag was not deleted. IsError *bool `json:"is_error,omitempty"` } @@ -705,10 +894,24 @@ type DetachTagOptions struct { // An array of tag names to detach. TagNames []string `json:"tag_names,omitempty"` + // The ID of the billing account where the resources to be un-tagged lives. It is a required parameter if `tag_type` is + // set to `service`, otherwise it is inferred from the authorization IAM token. + AccountID *string `json:"account_id,omitempty"` + + // The type of the tag. Supported values are `user` and `service`. `service` is not supported for `providers=ims`. + TagType *string `json:"tag_type,omitempty"` + // Allows users to set headers on API requests Headers map[string]string } +// Constants associated with the DetachTagOptions.TagType property. +// The type of the tag. Supported values are `user` and `service`. `service` is not supported for `providers=ims`. +const ( + DetachTagOptions_TagType_Service = "service" + DetachTagOptions_TagType_User = "user" +) + // NewDetachTagOptions : Instantiate DetachTagOptions func (*GlobalTaggingV1) NewDetachTagOptions(resources []Resource) *DetachTagOptions { return &DetachTagOptions{ @@ -734,6 +937,18 @@ func (options *DetachTagOptions) SetTagNames(tagNames []string) *DetachTagOption return options } +// SetAccountID : Allow user to set AccountID +func (options *DetachTagOptions) SetAccountID(accountID string) *DetachTagOptions { + options.AccountID = core.StringPtr(accountID) + return options +} + +// SetTagType : Allow user to set TagType +func (options *DetachTagOptions) SetTagType(tagType string) *DetachTagOptions { + options.TagType = core.StringPtr(tagType) + return options +} + // SetHeaders : Allow user to set Headers func (options *DetachTagOptions) SetHeaders(param map[string]string) *DetachTagOptions { options.Headers = param @@ -742,40 +957,55 @@ func (options *DetachTagOptions) SetHeaders(param map[string]string) *DetachTagO // ListTagsOptions : The ListTags options. type ListTagsOptions struct { - // Select a provider. Supported values are `ghost` and `ims`. To list GhoST tags and infrastructure tags use - // `ghost,ims`. - Providers []string `json:"providers,omitempty"` + // The ID of the billing account to list the tags for. If it is not set, then it is taken from the authorization token. + // This parameter is required if `tag_type` is set to `service`. + AccountID *string `json:"account_id,omitempty"` - // If you want to return only the list of tags attached to a specified resource, pass here the ID of the resource. For - // GhoST onboarded resources, the resource ID is the CRN; for IMS resources, it is the IMS ID. When using this - // parameter it is mandatory to specify the appropriate provider (`ims` or `ghost`). - AttachedTo *string `json:"attached_to,omitempty"` + // The type of the tag you want to list. Supported values are `user` and `service`. + TagType *string `json:"tag_type,omitempty"` // If set to `true`, this query returns the provider, `ghost`, `ims` or `ghost,ims`, where the tag exists and the // number of attached resources. FullData *bool `json:"full_data,omitempty"` + // Select a provider. Supported values are `ghost` and `ims`. To list GhoST tags and infrastructure tags use + // `ghost,ims`. `service` tags can only be attached to GhoST onboarded resources, so you don't need to set this + // parameter when listing `service` tags. + Providers []string `json:"providers,omitempty"` + + // If you want to return only the list of tags attached to a specified resource, pass the ID of the resource on this + // parameter. For GhoST onboarded resources, the resource ID is the CRN; for IMS resources, it is the IMS ID. When + // using this parameter, you must specify the appropriate provider (`ims` or `ghost`). + AttachedTo *string `json:"attached_to,omitempty"` + // The offset is the index of the item from which you want to start returning data from. Offset *int64 `json:"offset,omitempty"` // The number of tags to return. Limit *int64 `json:"limit,omitempty"` - // Order the output by tag name. - OrderByName *string `json:"order_by_name,omitempty"` - // The search timeout bounds the search request to be executed within the specified time value. It returns the hits // accumulated until time runs out. Timeout *int64 `json:"timeout,omitempty"` - // Filter on attached tags. If true, returns only tags that are attached to one or more resources. If false returns all - // tags. + // Order the output by tag name. + OrderByName *string `json:"order_by_name,omitempty"` + + // Filter on attached tags. If `true`, it returns only tags that are attached to one or more resources. If `false`, it + // returns all tags. AttachedOnly *bool `json:"attached_only,omitempty"` // Allows users to set headers on API requests Headers map[string]string } +// Constants associated with the ListTagsOptions.TagType property. +// The type of the tag you want to list. Supported values are `user` and `service`. +const ( + ListTagsOptions_TagType_Service = "service" + ListTagsOptions_TagType_User = "user" +) + // Constants associated with the ListTagsOptions.Providers property. const ( ListTagsOptions_Providers_Ghost = "ghost" @@ -794,15 +1024,15 @@ func (*GlobalTaggingV1) NewListTagsOptions() *ListTagsOptions { return &ListTagsOptions{} } -// SetProviders : Allow user to set Providers -func (options *ListTagsOptions) SetProviders(providers []string) *ListTagsOptions { - options.Providers = providers +// SetAccountID : Allow user to set AccountID +func (options *ListTagsOptions) SetAccountID(accountID string) *ListTagsOptions { + options.AccountID = core.StringPtr(accountID) return options } -// SetAttachedTo : Allow user to set AttachedTo -func (options *ListTagsOptions) SetAttachedTo(attachedTo string) *ListTagsOptions { - options.AttachedTo = core.StringPtr(attachedTo) +// SetTagType : Allow user to set TagType +func (options *ListTagsOptions) SetTagType(tagType string) *ListTagsOptions { + options.TagType = core.StringPtr(tagType) return options } @@ -812,6 +1042,18 @@ func (options *ListTagsOptions) SetFullData(fullData bool) *ListTagsOptions { return options } +// SetProviders : Allow user to set Providers +func (options *ListTagsOptions) SetProviders(providers []string) *ListTagsOptions { + options.Providers = providers + return options +} + +// SetAttachedTo : Allow user to set AttachedTo +func (options *ListTagsOptions) SetAttachedTo(attachedTo string) *ListTagsOptions { + options.AttachedTo = core.StringPtr(attachedTo) + return options +} + // SetOffset : Allow user to set Offset func (options *ListTagsOptions) SetOffset(offset int64) *ListTagsOptions { options.Offset = core.Int64Ptr(offset) @@ -824,18 +1066,18 @@ func (options *ListTagsOptions) SetLimit(limit int64) *ListTagsOptions { return options } -// SetOrderByName : Allow user to set OrderByName -func (options *ListTagsOptions) SetOrderByName(orderByName string) *ListTagsOptions { - options.OrderByName = core.StringPtr(orderByName) - return options -} - // SetTimeout : Allow user to set Timeout func (options *ListTagsOptions) SetTimeout(timeout int64) *ListTagsOptions { options.Timeout = core.Int64Ptr(timeout) return options } +// SetOrderByName : Allow user to set OrderByName +func (options *ListTagsOptions) SetOrderByName(orderByName string) *ListTagsOptions { + options.OrderByName = core.StringPtr(orderByName) + return options +} + // SetAttachedOnly : Allow user to set AttachedOnly func (options *ListTagsOptions) SetAttachedOnly(attachedOnly bool) *ListTagsOptions { options.AttachedOnly = core.BoolPtr(attachedOnly) @@ -882,6 +1124,81 @@ func UnmarshalResource(m map[string]json.RawMessage, result interface{}) (err er return } +// SetTagsOptions : The SetTags options. +type SetTagsOptions struct { + // An array of tag names to set. + TagNames []string `json:"tag_names" validate:"required"` + + // List of resources on which the tags should be set. Current implementation support only one resource whose type is + // included in a specific subset of IMS resources. + Resources []Resource `json:"resources" validate:"required"` + + // GhoST reserved. + XGhost *string `json:"X-Ghost,omitempty"` + + // The ID of the billing account where the resources to be tagged lives. + AccountID *string `json:"account_id,omitempty"` + + // Allows users to set headers on API requests + Headers map[string]string +} + +// NewSetTagsOptions : Instantiate SetTagsOptions +func (*GlobalTaggingV1) NewSetTagsOptions(tagNames []string, resources []Resource) *SetTagsOptions { + return &SetTagsOptions{ + TagNames: tagNames, + Resources: resources, + } +} + +// SetTagNames : Allow user to set TagNames +func (options *SetTagsOptions) SetTagNames(tagNames []string) *SetTagsOptions { + options.TagNames = tagNames + return options +} + +// SetResources : Allow user to set Resources +func (options *SetTagsOptions) SetResources(resources []Resource) *SetTagsOptions { + options.Resources = resources + return options +} + +// SetXGhost : Allow user to set XGhost +func (options *SetTagsOptions) SetXGhost(xGhost string) *SetTagsOptions { + options.XGhost = core.StringPtr(xGhost) + return options +} + +// SetAccountID : Allow user to set AccountID +func (options *SetTagsOptions) SetAccountID(accountID string) *SetTagsOptions { + options.AccountID = core.StringPtr(accountID) + return options +} + +// SetHeaders : Allow user to set Headers +func (options *SetTagsOptions) SetHeaders(param map[string]string) *SetTagsOptions { + options.Headers = param + return options +} + +// SetTagsResults : Results of a set_tags request. +type SetTagsResults struct { + // Array of results of an set_tags request. + Results []TagResultsItem `json:"results,omitempty"` +} + + +// UnmarshalSetTagsResults unmarshals an instance of SetTagsResults from the specified map of raw messages. +func UnmarshalSetTagsResults(m map[string]json.RawMessage, result interface{}) (err error) { + obj := new(SetTagsResults) + err = core.UnmarshalModel(m, "results", &obj.Results, UnmarshalTagResultsItem) + if err != nil { + return + } + reflect.ValueOf(result).Elem().Set(reflect.ValueOf(obj)) + return +} + // Tag : A tag. type Tag struct { // This is the name of the tag. @@ -902,16 +1219,16 @@ func UnmarshalTag(m map[string]json.RawMessage, result interface{}) (err error) // TagList : A list of tags. type TagList struct { - // The number of tags defined in the account. + // Set the occurrencies of the total tags associated to this account. TotalCount *int64 `json:"total_count,omitempty"` - // The offset specific at input time. + // The offset at which tags are returned. Offset *int64 `json:"offset,omitempty"` - // The limit specified at input time. + // The number of tags requested to be returned. Limit *int64 `json:"limit,omitempty"` - // This is an array of output results. + // Array of output results. Items []Tag `json:"items,omitempty"` } diff --git a/globaltaggingv1/global_tagging_v1_examples_test.go b/globaltaggingv1/global_tagging_v1_examples_test.go new file mode 100644 index 00000000..542bb165 --- /dev/null +++ b/globaltaggingv1/global_tagging_v1_examples_test.go @@ -0,0 +1,246 @@ +// +build examples + +/** + * (C) Copyright IBM Corp. 2020. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package globaltaggingv1_test + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/IBM/go-sdk-core/v4/core" + "github.com/IBM/platform-services-go-sdk/globaltaggingv1" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +// This file provides an example of how to use the Global Tagging service. +// +// The following configuration properties are assumed to be defined in the external configuration file: +// GLOBAL_TAGGING_URL= +// GLOBAL_TAGGING_AUTHTYPE=iam +// GLOBAL_TAGGING_APIKEY= +// GLOBAL_TAGGING_AUTH_URL= +// GLOBAL_TAGGING_RESOURCE_CRN= + +const externalConfigFile = "../global_tagging.env" + +var ( + globalTaggingService *globaltaggingv1.GlobalTaggingV1 + config map[string]string + configLoaded bool = false + + resourceCRN string +) + +func shouldSkipTest() { + if !configLoaded { + Skip("External configuration is not available, skipping tests...") + } +} + +var _ = Describe(`GlobalTaggingV1 Examples Tests`, func() { + Describe(`External configuration`, func() { + It("Successfully load the configuration", func() { + var err error + _, err = os.Stat(externalConfigFile) + if err != nil { + Skip("External configuration file not found, skipping tests: " + err.Error()) + } + + os.Setenv("IBM_CREDENTIALS_FILE", externalConfigFile) + config, err = core.GetServiceProperties(globaltaggingv1.DefaultServiceName) + if err != nil { + Skip("Error loading service properties, skipping tests: " + err.Error()) + } + + resourceCRN = config["RESOURCE_CRN"] + if resourceCRN == "" { + Skip("Unable to load RESOURCE_CRN configuration property, skipping tests") + } + + configLoaded = len(config) > 0 + }) + }) + + Describe(`Client initialization`, func() { + BeforeEach(func() { + shouldSkipTest() + }) + It("Successfully construct the service client instance", func() { + var err error + + // begin-common + + globalTaggingServiceOptions := &globaltaggingv1.GlobalTaggingV1Options{} + + globalTaggingService, err = globaltaggingv1.NewGlobalTaggingV1UsingExternalConfig(globalTaggingServiceOptions) + + if err != nil { + panic(err) + } + + // end-common + + Expect(globalTaggingService).ToNot(BeNil()) + }) + }) + + Describe(`GlobalTaggingV1 request examples`, func() { + BeforeEach(func() { + shouldSkipTest() + }) + It(`ListTags request example`, func() { + // begin-list_tags + + listTagsOptions := globalTaggingService.NewListTagsOptions() + + tagList, response, err := globalTaggingService.ListTags(listTagsOptions) + if err != nil { + panic(err) + } + b, _ := json.MarshalIndent(tagList, "", " ") + fmt.Printf("\nListTags() result:\n%s\n", string(b)) + + // end-list_tags + + Expect(err).To(BeNil()) + Expect(response.StatusCode).To(Equal(200)) + Expect(tagList).ToNot(BeNil()) + + }) + It(`AttachTag request example`, func() { + // begin-attach_tag + + resourceModel := &globaltaggingv1.Resource{ + ResourceID: &resourceCRN, + } + + attachTagOptions := globalTaggingService.NewAttachTagOptions( + []globaltaggingv1.Resource{*resourceModel}, + ) + attachTagOptions.SetTagNames([]string{"tag_test_1", "tag_test_2"}) + + tagResults, response, err := globalTaggingService.AttachTag(attachTagOptions) + if err != nil { + panic(err) + } + b, _ := json.MarshalIndent(tagResults, "", " ") + fmt.Printf("\nAttachTag() result:\n%s\n", string(b)) + + // end-attach_tag + + Expect(err).To(BeNil()) + Expect(response.StatusCode).To(Equal(200)) + Expect(tagResults).ToNot(BeNil()) + + }) + It(`DetachTag request example`, func() { + // begin-detach_tag + + resourceModel := &globaltaggingv1.Resource{ + ResourceID: &resourceCRN, + } + + detachTagOptions := globalTaggingService.NewDetachTagOptions( + []globaltaggingv1.Resource{*resourceModel}, + ) + detachTagOptions.SetTagNames([]string{"tag_test_1", "tag_test_2"}) + + tagResults, response, err := globalTaggingService.DetachTag(detachTagOptions) + if err != nil { + panic(err) + } + b, _ := json.MarshalIndent(tagResults, "", " ") + fmt.Printf("\nDetachTag() result:\n%s\n", string(b)) + + // end-detach_tag + + Expect(err).To(BeNil()) + Expect(response.StatusCode).To(Equal(200)) + Expect(tagResults).ToNot(BeNil()) + + }) + It(`SetTags request example`, func() { + // begin-set_tags + + resourceModel := &globaltaggingv1.Resource{ + ResourceID: &resourceCRN, + } + + setTagsOptions := globalTaggingService.NewSetTagsOptions( + []string{"tag_test_1", "tag_test_2"}, + []globaltaggingv1.Resource{*resourceModel}, + ) + + setTagsResults, response, err := globalTaggingService.SetTags(setTagsOptions) + if err != nil { + panic(err) + } + b, _ := json.MarshalIndent(setTagsResults, "", " ") + fmt.Printf("\nSetTags() result:\n%s\n", string(b)) + + // end-set_tags + + Expect(err).To(BeNil()) + Expect(response.StatusCode).To(Equal(200)) + Expect(setTagsResults).ToNot(BeNil()) + + }) + It(`DeleteTagAll request example`, func() { + // begin-delete_tag_all + + deleteTagAllOptions := globalTaggingService.NewDeleteTagAllOptions() + + deleteTagsResult, response, err := globalTaggingService.DeleteTagAll(deleteTagAllOptions) + if err != nil { + panic(err) + } + b, _ := json.MarshalIndent(deleteTagsResult, "", " ") + fmt.Printf("\nDeleteTagAll() result:\n%s\n", string(b)) + + // end-delete_tag_all + + Expect(err).To(BeNil()) + Expect(response.StatusCode).To(Equal(200)) + Expect(deleteTagsResult).ToNot(BeNil()) + + }) + It(`DeleteTag request example`, func() { + // begin-delete_tag + + deleteTagOptions := globalTaggingService.NewDeleteTagOptions( + "tag_test_1", + ) + + deleteTagResults, response, err := globalTaggingService.DeleteTag(deleteTagOptions) + if err != nil { + panic(err) + } + b, _ := json.MarshalIndent(deleteTagResults, "", " ") + fmt.Printf("\nDeleteTag() result:\n%s\n", string(b)) + + // end-delete_tag + + Expect(err).To(BeNil()) + Expect(response.StatusCode).To(Equal(200)) + Expect(deleteTagResults).ToNot(BeNil()) + + }) + }) +}) diff --git a/globaltaggingv1/global_tagging_v1_integration_test.go b/globaltaggingv1/global_tagging_v1_integration_test.go index 7f3d76e4..fa6d324b 100644 --- a/globaltaggingv1/global_tagging_v1_integration_test.go +++ b/globaltaggingv1/global_tagging_v1_integration_test.go @@ -1,7 +1,5 @@ // +build integration -package globaltaggingv1_test - /** * (C) Copyright IBM Corp. 2020. * @@ -18,167 +16,279 @@ package globaltaggingv1_test * limitations under the License. */ +package globaltaggingv1_test + import ( + "encoding/json" "fmt" "os" "time" - - "github.com/joho/godotenv" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - + "github.com/IBM/go-sdk-core/v4/core" "github.com/IBM/platform-services-go-sdk/globaltaggingv1" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" ) -type Resource = globaltaggingv1.Resource -type Tag = globaltaggingv1.Tag - -var service *globaltaggingv1.GlobalTaggingV1 -var configLoaded = false +/** + * This file contains an integration test for the globaltaggingv1 package. + * + * Notes: + * + * The integration test will automatically skip tests if the required config file is not available. + */ -var crn string +var _ = Describe(`GlobalTaggingV1 Integration Tests`, func() { -var tagName string = fmt.Sprint("go-sdk-", time.Now().Unix()) + const externalConfigFile = "../global_tagging.env" -var tagElem = Tag{ - Name: core.StringPtr(tagName), -} + var ( + err error + globalTaggingService *globaltaggingv1.GlobalTaggingV1 + serviceURL string + config map[string]string -const externalConfigFile = "../ghost.env" + crn string + tagName string = fmt.Sprint("go-sdk-", time.Now().Unix()) + ) -func shouldSkipTest() { - if !configLoaded { - Skip("External configuration is not available, skipping...") + var shouldSkipTest = func() { + Skip("External configuration is not available, skipping tests...") } -} -var _ = Describe("Global Search and Tagging - Tagging integration test", func() { - It("Successfully load the configuration", func() { - err := godotenv.Overload(externalConfigFile) - if err == nil { - crn = os.Getenv("GST_RESOURCE_CRN") - if crn != "" { - configLoaded = true + Describe(`External configuration`, func() { + It("Successfully load the configuration", func() { + _, err = os.Stat(externalConfigFile) + if err != nil { + Skip("External configuration file not found, skipping tests: " + err.Error()) } - } - if !configLoaded { - Skip("External configuration could not be loaded, skipping...") - } - }) - It("Successfully construct service", func() { - shouldSkipTest() - - // Create the authenticator. - authenticator := &core.IamAuthenticator{ - ApiKey: os.Getenv("GST_IINTERNA_APIKEY"), - URL: os.Getenv("GST_IAM_URL"), - } - - options := &globaltaggingv1.GlobalTaggingV1Options{ - Authenticator: authenticator, - URL: os.Getenv("GST_TAGS_URL"), - } - var err error - service, err = globaltaggingv1.NewGlobalTaggingV1(options) - Expect(err).To(BeNil()) - Expect(service).ToNot(BeNil()) - }) + os.Setenv("IBM_CREDENTIALS_FILE", externalConfigFile) + config, err = core.GetServiceProperties(globaltaggingv1.DefaultServiceName) + if err != nil { + Skip("Error loading service properties, skipping tests: " + err.Error()) + } + serviceURL = config["URL"] + if serviceURL == "" { + Skip("Unable to load service URL configuration property, skipping tests") + } - Describe("Call GetAllTags", func() { + crn = config["RESOURCE_CRN"] + if crn == "" { + Skip("Unable to load RESOURCE_CRN configuration property, skipping tests") + } + + fmt.Printf("\nService URL: %s\n", serviceURL) + shouldSkipTest = func() {} + }) + }) - It("Successfully get all tags", func() { + Describe(`Client initialization`, func() { + BeforeEach(func() { shouldSkipTest() + }) + It("Successfully construct the service client instance", func() { + + globalTaggingServiceOptions := &globaltaggingv1.GlobalTaggingV1Options{} - // Construct an instance of the ListTags model - listTagsModel := service.NewListTagsOptions() + globalTaggingService, err = globaltaggingv1.NewGlobalTaggingV1UsingExternalConfig(globalTaggingServiceOptions) - var err error - result, detailedResponse, err := service.ListTags(listTagsModel) Expect(err).To(BeNil()) - Expect(detailedResponse.StatusCode).To(Equal(200)) - Expect(result.Items).NotTo(BeEmpty()) + Expect(globalTaggingService).ToNot(BeNil()) + Expect(globalTaggingService.Service.Options.URL).To(Equal(serviceURL)) }) }) - Describe("Attach a tag", func() { + Describe(`ListTags - Get all tags`, func() { + BeforeEach(func() { + shouldSkipTest() + }) + It(`ListTags(listTagsOptions *ListTagsOptions)`, func() { + + listTagsOptions := &globaltaggingv1.ListTagsOptions{ + Offset: core.Int64Ptr(0), + Limit: core.Int64Ptr(1000), + } + + tagList, response, err := globalTaggingService.ListTags(listTagsOptions) - It("Successfully attach a tag", func() { + Expect(err).To(BeNil()) + Expect(response.StatusCode).To(Equal(200)) + Expect(tagList).ToNot(BeNil()) + // fmt.Printf("\nListTags(all) response:\n%s", toJson(tagList)) + + Expect(tagList.Items).ToNot(BeEmpty()) + }) + }) + + Describe(`AttachTag - Attach tags`, func() { + BeforeEach(func() { shouldSkipTest() + }) + It(`AttachTag(attachTagOptions *AttachTagOptions)`, func() { - // Construct an instance of the Resource model - resource, _ := service.NewResource(crn) - array := []Resource{*resource} - attachTagOptions := service.NewAttachTagOptions(array) - attachTagOptions.SetTagNames([]string{tagName}) + resourceModel := &globaltaggingv1.Resource{ + ResourceID: &crn, + } - var err error - result, detailedResponse, err := service.AttachTag(attachTagOptions) + attachTagOptions := &globaltaggingv1.AttachTagOptions{ + Resources: []globaltaggingv1.Resource{*resourceModel}, + TagNames: []string{tagName}, + } - //fmt.Println("attach tag response", detailedResponse) + tagResults, response, err := globalTaggingService.AttachTag(attachTagOptions) Expect(err).To(BeNil()) - Expect(detailedResponse.StatusCode).To(Equal(200)) - for _, elem := range result.Results { + Expect(response.StatusCode).To(Equal(200)) + Expect(tagResults).ToNot(BeNil()) + // fmt.Printf("\nAttachTag() response:\n%s", toJson(tagResults)) + + Expect(tagResults.Results).ToNot(BeEmpty()) + for _, elem := range tagResults.Results { Expect(*elem.IsError).To(Equal(false)) } - // checking that tag is attached - listTagsModel := service.NewListTagsOptions() - listTagsModel.SetAttachedTo(crn) - resultCheck, detailedResponseCheck, errCheck := service.ListTags(listTagsModel) - Expect(errCheck).To(BeNil()) - Expect(detailedResponseCheck.StatusCode).To(Equal(200)) - Expect(resultCheck.Items).NotTo(BeEmpty()) - Expect(resultCheck.Items).To(ContainElement(tagElem)) + // Make sure the tag was in fact attached. + tagNames := getTagNamesForResource(globalTaggingService, crn) + // fmt.Print("\nResource now has these tags: ", tagNames) + Expect(tagNames).ToNot(BeEmpty()) + Expect(tagNames).To(ContainElement(tagName)) }) }) - Describe("Detach a tag", func() { - - It("Successfully detached a tag", func() { + Describe(`DetachTag - Detach tags`, func() { + BeforeEach(func() { shouldSkipTest() + }) + It(`DetachTag(detachTagOptions *DetachTagOptions)`, func() { + + resourceModel := &globaltaggingv1.Resource{ + ResourceID: &crn, + } + + detachTagOptions := &globaltaggingv1.DetachTagOptions{ + Resources: []globaltaggingv1.Resource{*resourceModel}, + TagNames: []string{tagName}, + } - // Construct an instance of the DetachTag model - resource, _ := service.NewResource(crn) - array := []Resource{*resource} - detachTagOptions := service.NewDetachTagOptions(array) - detachTagOptions.SetTagNames([]string{tagName}) + tagResults, response, err := globalTaggingService.DetachTag(detachTagOptions) - var err error - result, detailedResponse, err := service.DetachTag(detachTagOptions) Expect(err).To(BeNil()) - Expect(detailedResponse.StatusCode).To(Equal(200)) - for _, elem := range result.Results { + Expect(response.StatusCode).To(Equal(200)) + Expect(tagResults).ToNot(BeNil()) + // fmt.Printf("\nDetachTag() response:\n%s", toJson(tagResults)) + + Expect(tagResults.Results).ToNot(BeEmpty()) + for _, elem := range tagResults.Results { Expect(*elem.IsError).To(Equal(false)) } - // checking that tag is detached - listTagsModel := service.NewListTagsOptions() - listTagsModel.SetAttachedTo(crn) - resultCheck, detailedResponseCheck, errCheck := service.ListTags(listTagsModel) - Expect(errCheck).To(BeNil()) - Expect(detailedResponseCheck.StatusCode).To(Equal(200)) - Expect(resultCheck.Items).NotTo(ContainElement(tagElem)) + // Make sure the tag was in fact detached. + tagNames := getTagNamesForResource(globalTaggingService, crn) + // fmt.Print("\nResource now has these tags: ", tagNames) + Expect(tagNames).ToNot(ContainElement(tagName)) }) }) - Describe("Delete a tag", func() { - - It("Successfully delete a tag", func() { + Describe(`DeleteTag - Delete an unused tag`, func() { + BeforeEach(func() { shouldSkipTest() + }) + It(`DeleteTag(deleteTagOptions *DeleteTagOptions)`, func() { + + deleteTagOptions := &globaltaggingv1.DeleteTagOptions{ + TagName: &tagName, + } - // Construct an instance of the DeleteTag model - deleteTagOptions := service.NewDeleteTagOptions(tagName) + deleteTagResults, response, err := globalTaggingService.DeleteTag(deleteTagOptions) - var err error - result, detailedResponse, err := service.DeleteTag(deleteTagOptions) Expect(err).To(BeNil()) - Expect(detailedResponse.StatusCode).To(Equal(200)) - for _, elem := range result.Results { + Expect(response.StatusCode).To(Equal(200)) + Expect(deleteTagResults).ToNot(BeNil()) + for _, elem := range deleteTagResults.Results { Expect(*elem.IsError).To(Equal(false)) } }) }) + + // Describe(`SetTags - Set tags for a resource`, func() { + // BeforeEach(func() { + // shouldSkipTest() + // }) + // It(`SetTags(setTagsOptions *SetTagsOptions)`, func() { + + // resourceModel := &globaltaggingv1.Resource{ + // ResourceID: &crn, + // } + + // setTagsOptions := &globaltaggingv1.SetTagsOptions{ + // TagNames: []string{tagName}, + // Resources: []globaltaggingv1.Resource{*resourceModel}, + // } + + // setTagsResults, response, err := globalTaggingService.SetTags(setTagsOptions) + + // Expect(err).To(BeNil()) + // Expect(response.StatusCode).To(Equal(200)) + // Expect(setTagsResults).ToNot(BeNil()) + // fmt.Printf("\nSetTags() response:\n%s", toJson(setTagsResults)) + // Expect(setTagsResults.Results).ToNot(BeEmpty()) + // for _, elem := range setTagsResults.Results { + // Expect(*elem.IsError).To(Equal(false)) + // } + + // // Make sure we end up with only the one tag set on the resource. + // tagNames := getTagNamesForResource(globalTaggingService, crn) + // fmt.Print("\nResource now has these tags: ", tagNames) + // Expect(tagNames).ToNot(BeEmpty()) + // Expect(len(tagNames)).To(Equal(1)) + // Expect(tagNames).To(ContainElement(tagName)) + // }) + // }) + + // Describe(`DeleteTagAll - Delete all unused tags`, func() { + // BeforeEach(func() { + // shouldSkipTest() + // }) + // It(`DeleteTagAll(deleteTagAllOptions *DeleteTagAllOptions)`, func() { + + // deleteTagAllOptions := &globaltaggingv1.DeleteTagAllOptions{ + // Providers: core.StringPtr("ghost"), + // AccountID: core.StringPtr("testString"), + // TagType: core.StringPtr("user"), + // } + + // deleteTagsResult, response, err := globalTaggingService.DeleteTagAll(deleteTagAllOptions) + + // Expect(err).To(BeNil()) + // Expect(response.StatusCode).To(Equal(200)) + // Expect(deleteTagsResult).ToNot(BeNil()) + + // }) + // }) }) + +func getTagNamesForResource(service *globaltaggingv1.GlobalTaggingV1, resourceID string) []string { + listTagsOptions := &globaltaggingv1.ListTagsOptions{ + AttachedTo: &resourceID, + } + tagList, response, err := service.ListTags(listTagsOptions) + Expect(err).To(BeNil()) + Expect(response.StatusCode).To(Equal(200)) + + tagNames := []string{} + for _, tag := range tagList.Items { + tagNames = append(tagNames, *tag.Name) + } + + return tagNames +} + +func toJson(obj interface{}) string { + b, err := json.MarshalIndent(obj, "", " ") + if err != nil { + panic(err) + } + return string(b) + +} diff --git a/globaltaggingv1/global_tagging_v1_test.go b/globaltaggingv1/global_tagging_v1_test.go index d86a7bd6..85426d25 100644 --- a/globaltaggingv1/global_tagging_v1_test.go +++ b/globaltaggingv1/global_tagging_v1_test.go @@ -144,18 +144,22 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Verify the contents of the request Expect(req.URL.EscapedPath()).To(Equal(listTagsPath)) Expect(req.Method).To(Equal("GET")) - Expect(req.URL.Query()["attached_to"]).To(Equal([]string{"testString"})) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"})) // TODO: Add check for full_data query parameter + Expect(req.URL.Query()["attached_to"]).To(Equal([]string{"testString"})) + Expect(req.URL.Query()["offset"]).To(Equal([]string{fmt.Sprint(int64(0))})) Expect(req.URL.Query()["limit"]).To(Equal([]string{fmt.Sprint(int64(1))})) - Expect(req.URL.Query()["order_by_name"]).To(Equal([]string{"asc"})) + Expect(req.URL.Query()["timeout"]).To(Equal([]string{fmt.Sprint(int64(0))})) - Expect(req.URL.Query()["timeout"]).To(Equal([]string{fmt.Sprint(int64(38))})) + Expect(req.URL.Query()["order_by_name"]).To(Equal([]string{"asc"})) // TODO: Add check for attached_only query parameter @@ -175,13 +179,15 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Construct an instance of the ListTagsOptions model listTagsOptionsModel := new(globaltaggingv1.ListTagsOptions) + listTagsOptionsModel.AccountID = core.StringPtr("testString") + listTagsOptionsModel.TagType = core.StringPtr("user") + listTagsOptionsModel.FullData = core.BoolPtr(true) listTagsOptionsModel.Providers = []string{"ghost"} listTagsOptionsModel.AttachedTo = core.StringPtr("testString") - listTagsOptionsModel.FullData = core.BoolPtr(true) listTagsOptionsModel.Offset = core.Int64Ptr(int64(0)) listTagsOptionsModel.Limit = core.Int64Ptr(int64(1)) + listTagsOptionsModel.Timeout = core.Int64Ptr(int64(0)) listTagsOptionsModel.OrderByName = core.StringPtr("asc") - listTagsOptionsModel.Timeout = core.Int64Ptr(int64(38)) listTagsOptionsModel.AttachedOnly = core.BoolPtr(true) listTagsOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Expect response parsing to fail since we are receiving a text/plain response @@ -206,25 +212,29 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Verify the contents of the request Expect(req.URL.EscapedPath()).To(Equal(listTagsPath)) Expect(req.Method).To(Equal("GET")) - Expect(req.URL.Query()["attached_to"]).To(Equal([]string{"testString"})) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"})) // TODO: Add check for full_data query parameter + Expect(req.URL.Query()["attached_to"]).To(Equal([]string{"testString"})) + Expect(req.URL.Query()["offset"]).To(Equal([]string{fmt.Sprint(int64(0))})) Expect(req.URL.Query()["limit"]).To(Equal([]string{fmt.Sprint(int64(1))})) - Expect(req.URL.Query()["order_by_name"]).To(Equal([]string{"asc"})) + Expect(req.URL.Query()["timeout"]).To(Equal([]string{fmt.Sprint(int64(0))})) - Expect(req.URL.Query()["timeout"]).To(Equal([]string{fmt.Sprint(int64(38))})) + Expect(req.URL.Query()["order_by_name"]).To(Equal([]string{"asc"})) // TODO: Add check for attached_only query parameter res.Header().Set("Content-type", "application/json") res.WriteHeader(200) - fmt.Fprintf(res, "%s", `{"total_count": 10, "offset": 6, "limit": 5, "items": [{"name": "Name"}]}`) + fmt.Fprintf(res, "%s", `{"total_count": 0, "offset": 0, "limit": 1, "items": [{"name": "Name"}]}`) })) }) It(`Invoke ListTags successfully`, func() { @@ -243,13 +253,15 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Construct an instance of the ListTagsOptions model listTagsOptionsModel := new(globaltaggingv1.ListTagsOptions) + listTagsOptionsModel.AccountID = core.StringPtr("testString") + listTagsOptionsModel.TagType = core.StringPtr("user") + listTagsOptionsModel.FullData = core.BoolPtr(true) listTagsOptionsModel.Providers = []string{"ghost"} listTagsOptionsModel.AttachedTo = core.StringPtr("testString") - listTagsOptionsModel.FullData = core.BoolPtr(true) listTagsOptionsModel.Offset = core.Int64Ptr(int64(0)) listTagsOptionsModel.Limit = core.Int64Ptr(int64(1)) + listTagsOptionsModel.Timeout = core.Int64Ptr(int64(0)) listTagsOptionsModel.OrderByName = core.StringPtr("asc") - listTagsOptionsModel.Timeout = core.Int64Ptr(int64(38)) listTagsOptionsModel.AttachedOnly = core.BoolPtr(true) listTagsOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} @@ -269,13 +281,15 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Construct an instance of the ListTagsOptions model listTagsOptionsModel := new(globaltaggingv1.ListTagsOptions) + listTagsOptionsModel.AccountID = core.StringPtr("testString") + listTagsOptionsModel.TagType = core.StringPtr("user") + listTagsOptionsModel.FullData = core.BoolPtr(true) listTagsOptionsModel.Providers = []string{"ghost"} listTagsOptionsModel.AttachedTo = core.StringPtr("testString") - listTagsOptionsModel.FullData = core.BoolPtr(true) listTagsOptionsModel.Offset = core.Int64Ptr(int64(0)) listTagsOptionsModel.Limit = core.Int64Ptr(int64(1)) + listTagsOptionsModel.Timeout = core.Int64Ptr(int64(0)) listTagsOptionsModel.OrderByName = core.StringPtr("asc") - listTagsOptionsModel.Timeout = core.Int64Ptr(int64(38)) listTagsOptionsModel.AttachedOnly = core.BoolPtr(true) listTagsOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Invoke operation with empty URL (negative test) @@ -304,6 +318,10 @@ var _ = Describe(`GlobalTaggingV1`, func() { Expect(req.Method).To(Equal("DELETE")) Expect(req.URL.Query()["providers"]).To(Equal([]string{"ghost"})) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"})) + res.Header().Set("Content-type", "application/json") res.WriteHeader(200) fmt.Fprintf(res, `} this is not valid json {`) @@ -320,6 +338,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Construct an instance of the DeleteTagAllOptions model deleteTagAllOptionsModel := new(globaltaggingv1.DeleteTagAllOptions) deleteTagAllOptionsModel.Providers = core.StringPtr("ghost") + deleteTagAllOptionsModel.AccountID = core.StringPtr("testString") + deleteTagAllOptionsModel.TagType = core.StringPtr("user") deleteTagAllOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Expect response parsing to fail since we are receiving a text/plain response result, response, operationErr := globalTaggingService.DeleteTagAll(deleteTagAllOptionsModel) @@ -345,6 +365,10 @@ var _ = Describe(`GlobalTaggingV1`, func() { Expect(req.Method).To(Equal("DELETE")) Expect(req.URL.Query()["providers"]).To(Equal([]string{"ghost"})) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"})) + res.Header().Set("Content-type", "application/json") res.WriteHeader(200) fmt.Fprintf(res, "%s", `{"total_count": 10, "errors": true, "items": [{"tag_name": "TagName", "is_error": false}]}`) @@ -367,6 +391,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Construct an instance of the DeleteTagAllOptions model deleteTagAllOptionsModel := new(globaltaggingv1.DeleteTagAllOptions) deleteTagAllOptionsModel.Providers = core.StringPtr("ghost") + deleteTagAllOptionsModel.AccountID = core.StringPtr("testString") + deleteTagAllOptionsModel.TagType = core.StringPtr("user") deleteTagAllOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Invoke operation with valid options model (positive test) @@ -386,6 +412,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Construct an instance of the DeleteTagAllOptions model deleteTagAllOptionsModel := new(globaltaggingv1.DeleteTagAllOptions) deleteTagAllOptionsModel.Providers = core.StringPtr("ghost") + deleteTagAllOptionsModel.AccountID = core.StringPtr("testString") + deleteTagAllOptionsModel.TagType = core.StringPtr("user") deleteTagAllOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Invoke operation with empty URL (negative test) err := globalTaggingService.SetServiceURL("") @@ -411,6 +439,10 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Verify the contents of the request Expect(req.URL.EscapedPath()).To(Equal(deleteTagPath)) Expect(req.Method).To(Equal("DELETE")) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"})) + res.Header().Set("Content-type", "application/json") res.WriteHeader(200) fmt.Fprintf(res, `} this is not valid json {`) @@ -428,6 +460,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { deleteTagOptionsModel := new(globaltaggingv1.DeleteTagOptions) deleteTagOptionsModel.TagName = core.StringPtr("testString") deleteTagOptionsModel.Providers = []string{"ghost"} + deleteTagOptionsModel.AccountID = core.StringPtr("testString") + deleteTagOptionsModel.TagType = core.StringPtr("user") deleteTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Expect response parsing to fail since we are receiving a text/plain response result, response, operationErr := globalTaggingService.DeleteTag(deleteTagOptionsModel) @@ -451,6 +485,10 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Verify the contents of the request Expect(req.URL.EscapedPath()).To(Equal(deleteTagPath)) Expect(req.Method).To(Equal("DELETE")) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"})) + res.Header().Set("Content-type", "application/json") res.WriteHeader(200) fmt.Fprintf(res, "%s", `{"results": [{"provider": "ghost", "is_error": false}]}`) @@ -474,6 +512,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { deleteTagOptionsModel := new(globaltaggingv1.DeleteTagOptions) deleteTagOptionsModel.TagName = core.StringPtr("testString") deleteTagOptionsModel.Providers = []string{"ghost"} + deleteTagOptionsModel.AccountID = core.StringPtr("testString") + deleteTagOptionsModel.TagType = core.StringPtr("user") deleteTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Invoke operation with valid options model (positive test) @@ -494,6 +534,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { deleteTagOptionsModel := new(globaltaggingv1.DeleteTagOptions) deleteTagOptionsModel.TagName = core.StringPtr("testString") deleteTagOptionsModel.Providers = []string{"ghost"} + deleteTagOptionsModel.AccountID = core.StringPtr("testString") + deleteTagOptionsModel.TagType = core.StringPtr("user") deleteTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Invoke operation with empty URL (negative test) err := globalTaggingService.SetServiceURL("") @@ -526,6 +568,10 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Verify the contents of the request Expect(req.URL.EscapedPath()).To(Equal(attachTagPath)) Expect(req.Method).To(Equal("POST")) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"})) + res.Header().Set("Content-type", "application/json") res.WriteHeader(200) fmt.Fprintf(res, `} this is not valid json {`) @@ -549,6 +595,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { attachTagOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} attachTagOptionsModel.TagName = core.StringPtr("testString") attachTagOptionsModel.TagNames = []string{"testString"} + attachTagOptionsModel.AccountID = core.StringPtr("testString") + attachTagOptionsModel.TagType = core.StringPtr("user") attachTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Expect response parsing to fail since we are receiving a text/plain response result, response, operationErr := globalTaggingService.AttachTag(attachTagOptionsModel) @@ -572,6 +620,10 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Verify the contents of the request Expect(req.URL.EscapedPath()).To(Equal(attachTagPath)) Expect(req.Method).To(Equal("POST")) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"})) + res.Header().Set("Content-type", "application/json") res.WriteHeader(200) fmt.Fprintf(res, "%s", `{"results": [{"resource_id": "ResourceID", "is_error": false}]}`) @@ -601,6 +653,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { attachTagOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} attachTagOptionsModel.TagName = core.StringPtr("testString") attachTagOptionsModel.TagNames = []string{"testString"} + attachTagOptionsModel.AccountID = core.StringPtr("testString") + attachTagOptionsModel.TagType = core.StringPtr("user") attachTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Invoke operation with valid options model (positive test) @@ -627,6 +681,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { attachTagOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} attachTagOptionsModel.TagName = core.StringPtr("testString") attachTagOptionsModel.TagNames = []string{"testString"} + attachTagOptionsModel.AccountID = core.StringPtr("testString") + attachTagOptionsModel.TagType = core.StringPtr("user") attachTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Invoke operation with empty URL (negative test) err := globalTaggingService.SetServiceURL("") @@ -659,6 +715,10 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Verify the contents of the request Expect(req.URL.EscapedPath()).To(Equal(detachTagPath)) Expect(req.Method).To(Equal("POST")) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"})) + res.Header().Set("Content-type", "application/json") res.WriteHeader(200) fmt.Fprintf(res, `} this is not valid json {`) @@ -682,6 +742,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { detachTagOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} detachTagOptionsModel.TagName = core.StringPtr("testString") detachTagOptionsModel.TagNames = []string{"testString"} + detachTagOptionsModel.AccountID = core.StringPtr("testString") + detachTagOptionsModel.TagType = core.StringPtr("user") detachTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Expect response parsing to fail since we are receiving a text/plain response result, response, operationErr := globalTaggingService.DetachTag(detachTagOptionsModel) @@ -705,6 +767,10 @@ var _ = Describe(`GlobalTaggingV1`, func() { // Verify the contents of the request Expect(req.URL.EscapedPath()).To(Equal(detachTagPath)) Expect(req.Method).To(Equal("POST")) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"})) + res.Header().Set("Content-type", "application/json") res.WriteHeader(200) fmt.Fprintf(res, "%s", `{"results": [{"resource_id": "ResourceID", "is_error": false}]}`) @@ -734,6 +800,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { detachTagOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} detachTagOptionsModel.TagName = core.StringPtr("testString") detachTagOptionsModel.TagNames = []string{"testString"} + detachTagOptionsModel.AccountID = core.StringPtr("testString") + detachTagOptionsModel.TagType = core.StringPtr("user") detachTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Invoke operation with valid options model (positive test) @@ -760,6 +828,8 @@ var _ = Describe(`GlobalTaggingV1`, func() { detachTagOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} detachTagOptionsModel.TagName = core.StringPtr("testString") detachTagOptionsModel.TagNames = []string{"testString"} + detachTagOptionsModel.AccountID = core.StringPtr("testString") + detachTagOptionsModel.TagType = core.StringPtr("user") detachTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} // Invoke operation with empty URL (negative test) err := globalTaggingService.SetServiceURL("") @@ -782,6 +852,150 @@ var _ = Describe(`GlobalTaggingV1`, func() { }) }) }) + Describe(`SetTags(setTagsOptions *SetTagsOptions) - Operation response error`, func() { + setTagsPath := "/v3/tags/set_tags" + Context(`Using mock server endpoint`, func() { + BeforeEach(func() { + testServer = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + defer GinkgoRecover() + + // Verify the contents of the request + Expect(req.URL.EscapedPath()).To(Equal(setTagsPath)) + Expect(req.Method).To(Equal("POST")) + Expect(req.Header["X-Ghost"]).ToNot(BeNil()) + Expect(req.Header["X-Ghost"][0]).To(Equal(fmt.Sprintf("%v", "testString"))) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + res.Header().Set("Content-type", "application/json") + res.WriteHeader(200) + fmt.Fprintf(res, `} this is not valid json {`) + })) + }) + It(`Invoke SetTags with error: Operation response processing error`, func() { + globalTaggingService, serviceErr := globaltaggingv1.NewGlobalTaggingV1(&globaltaggingv1.GlobalTaggingV1Options{ + URL: testServer.URL, + Authenticator: &core.NoAuthAuthenticator{}, + }) + Expect(serviceErr).To(BeNil()) + Expect(globalTaggingService).ToNot(BeNil()) + + // Construct an instance of the Resource model + resourceModel := new(globaltaggingv1.Resource) + resourceModel.ResourceID = core.StringPtr("testString") + resourceModel.ResourceType = core.StringPtr("testString") + + // Construct an instance of the SetTagsOptions model + setTagsOptionsModel := new(globaltaggingv1.SetTagsOptions) + setTagsOptionsModel.TagNames = []string{"testString"} + setTagsOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} + setTagsOptionsModel.XGhost = core.StringPtr("testString") + setTagsOptionsModel.AccountID = core.StringPtr("testString") + setTagsOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} + // Expect response parsing to fail since we are receiving a text/plain response + result, response, operationErr := globalTaggingService.SetTags(setTagsOptionsModel) + Expect(operationErr).ToNot(BeNil()) + Expect(response).ToNot(BeNil()) + Expect(result).To(BeNil()) + }) + AfterEach(func() { + testServer.Close() + }) + }) + }) + + Describe(`SetTags(setTagsOptions *SetTagsOptions)`, func() { + setTagsPath := "/v3/tags/set_tags" + Context(`Using mock server endpoint`, func() { + BeforeEach(func() { + testServer = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + defer GinkgoRecover() + + // Verify the contents of the request + Expect(req.URL.EscapedPath()).To(Equal(setTagsPath)) + Expect(req.Method).To(Equal("POST")) + Expect(req.Header["X-Ghost"]).ToNot(BeNil()) + Expect(req.Header["X-Ghost"][0]).To(Equal(fmt.Sprintf("%v", "testString"))) + Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) + + res.Header().Set("Content-type", "application/json") + res.WriteHeader(200) + fmt.Fprintf(res, "%s", `{"results": [{"resource_id": "ResourceID", "is_error": false}]}`) + })) + }) + It(`Invoke SetTags successfully`, func() { + globalTaggingService, serviceErr := globaltaggingv1.NewGlobalTaggingV1(&globaltaggingv1.GlobalTaggingV1Options{ + URL: testServer.URL, + Authenticator: &core.NoAuthAuthenticator{}, + }) + Expect(serviceErr).To(BeNil()) + Expect(globalTaggingService).ToNot(BeNil()) + + // Invoke operation with nil options model (negative test) + result, response, operationErr := globalTaggingService.SetTags(nil) + Expect(operationErr).NotTo(BeNil()) + Expect(response).To(BeNil()) + Expect(result).To(BeNil()) + + // Construct an instance of the Resource model + resourceModel := new(globaltaggingv1.Resource) + resourceModel.ResourceID = core.StringPtr("testString") + resourceModel.ResourceType = core.StringPtr("testString") + + // Construct an instance of the SetTagsOptions model + setTagsOptionsModel := new(globaltaggingv1.SetTagsOptions) + setTagsOptionsModel.TagNames = []string{"testString"} + setTagsOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} + setTagsOptionsModel.XGhost = core.StringPtr("testString") + setTagsOptionsModel.AccountID = core.StringPtr("testString") + setTagsOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} + + // Invoke operation with valid options model (positive test) + result, response, operationErr = globalTaggingService.SetTags(setTagsOptionsModel) + Expect(operationErr).To(BeNil()) + Expect(response).ToNot(BeNil()) + Expect(result).ToNot(BeNil()) + }) + It(`Invoke SetTags with error: Operation validation and request error`, func() { + globalTaggingService, serviceErr := globaltaggingv1.NewGlobalTaggingV1(&globaltaggingv1.GlobalTaggingV1Options{ + URL: testServer.URL, + Authenticator: &core.NoAuthAuthenticator{}, + }) + Expect(serviceErr).To(BeNil()) + Expect(globalTaggingService).ToNot(BeNil()) + + // Construct an instance of the Resource model + resourceModel := new(globaltaggingv1.Resource) + resourceModel.ResourceID = core.StringPtr("testString") + resourceModel.ResourceType = core.StringPtr("testString") + + // Construct an instance of the SetTagsOptions model + setTagsOptionsModel := new(globaltaggingv1.SetTagsOptions) + setTagsOptionsModel.TagNames = []string{"testString"} + setTagsOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} + setTagsOptionsModel.XGhost = core.StringPtr("testString") + setTagsOptionsModel.AccountID = core.StringPtr("testString") + setTagsOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} + // Invoke operation with empty URL (negative test) + err := globalTaggingService.SetServiceURL("") + Expect(err).To(BeNil()) + result, response, operationErr := globalTaggingService.SetTags(setTagsOptionsModel) + Expect(operationErr).ToNot(BeNil()) + Expect(operationErr.Error()).To(ContainSubstring(core.ERRORMSG_SERVICE_URL_MISSING)) + Expect(response).To(BeNil()) + Expect(result).To(BeNil()) + // Construct a second instance of the SetTagsOptions model with no property values + setTagsOptionsModelNew := new(globaltaggingv1.SetTagsOptions) + // Invoke operation with invalid model (negative test) + result, response, operationErr = globalTaggingService.SetTags(setTagsOptionsModelNew) + Expect(operationErr).ToNot(BeNil()) + Expect(response).To(BeNil()) + Expect(result).To(BeNil()) + }) + AfterEach(func() { + testServer.Close() + }) + }) + }) Describe(`Model constructor tests`, func() { Context(`Using a service client instance`, func() { globalTaggingService, _ := globaltaggingv1.NewGlobalTaggingV1(&globaltaggingv1.GlobalTaggingV1Options{ @@ -803,20 +1017,28 @@ var _ = Describe(`GlobalTaggingV1`, func() { attachTagOptionsModel.SetResources([]globaltaggingv1.Resource{*resourceModel}) attachTagOptionsModel.SetTagName("testString") attachTagOptionsModel.SetTagNames([]string{"testString"}) + attachTagOptionsModel.SetAccountID("testString") + attachTagOptionsModel.SetTagType("user") attachTagOptionsModel.SetHeaders(map[string]string{"foo": "bar"}) Expect(attachTagOptionsModel).ToNot(BeNil()) Expect(attachTagOptionsModel.Resources).To(Equal([]globaltaggingv1.Resource{*resourceModel})) Expect(attachTagOptionsModel.TagName).To(Equal(core.StringPtr("testString"))) Expect(attachTagOptionsModel.TagNames).To(Equal([]string{"testString"})) + Expect(attachTagOptionsModel.AccountID).To(Equal(core.StringPtr("testString"))) + Expect(attachTagOptionsModel.TagType).To(Equal(core.StringPtr("user"))) Expect(attachTagOptionsModel.Headers).To(Equal(map[string]string{"foo": "bar"})) }) It(`Invoke NewDeleteTagAllOptions successfully`, func() { // Construct an instance of the DeleteTagAllOptions model deleteTagAllOptionsModel := globalTaggingService.NewDeleteTagAllOptions() deleteTagAllOptionsModel.SetProviders("ghost") + deleteTagAllOptionsModel.SetAccountID("testString") + deleteTagAllOptionsModel.SetTagType("user") deleteTagAllOptionsModel.SetHeaders(map[string]string{"foo": "bar"}) Expect(deleteTagAllOptionsModel).ToNot(BeNil()) Expect(deleteTagAllOptionsModel.Providers).To(Equal(core.StringPtr("ghost"))) + Expect(deleteTagAllOptionsModel.AccountID).To(Equal(core.StringPtr("testString"))) + Expect(deleteTagAllOptionsModel.TagType).To(Equal(core.StringPtr("user"))) Expect(deleteTagAllOptionsModel.Headers).To(Equal(map[string]string{"foo": "bar"})) }) It(`Invoke NewDeleteTagOptions successfully`, func() { @@ -825,10 +1047,14 @@ var _ = Describe(`GlobalTaggingV1`, func() { deleteTagOptionsModel := globalTaggingService.NewDeleteTagOptions(tagName) deleteTagOptionsModel.SetTagName("testString") deleteTagOptionsModel.SetProviders([]string{"ghost"}) + deleteTagOptionsModel.SetAccountID("testString") + deleteTagOptionsModel.SetTagType("user") deleteTagOptionsModel.SetHeaders(map[string]string{"foo": "bar"}) Expect(deleteTagOptionsModel).ToNot(BeNil()) Expect(deleteTagOptionsModel.TagName).To(Equal(core.StringPtr("testString"))) Expect(deleteTagOptionsModel.Providers).To(Equal([]string{"ghost"})) + Expect(deleteTagOptionsModel.AccountID).To(Equal(core.StringPtr("testString"))) + Expect(deleteTagOptionsModel.TagType).To(Equal(core.StringPtr("user"))) Expect(deleteTagOptionsModel.Headers).To(Equal(map[string]string{"foo": "bar"})) }) It(`Invoke NewDetachTagOptions successfully`, func() { @@ -846,33 +1072,41 @@ var _ = Describe(`GlobalTaggingV1`, func() { detachTagOptionsModel.SetResources([]globaltaggingv1.Resource{*resourceModel}) detachTagOptionsModel.SetTagName("testString") detachTagOptionsModel.SetTagNames([]string{"testString"}) + detachTagOptionsModel.SetAccountID("testString") + detachTagOptionsModel.SetTagType("user") detachTagOptionsModel.SetHeaders(map[string]string{"foo": "bar"}) Expect(detachTagOptionsModel).ToNot(BeNil()) Expect(detachTagOptionsModel.Resources).To(Equal([]globaltaggingv1.Resource{*resourceModel})) Expect(detachTagOptionsModel.TagName).To(Equal(core.StringPtr("testString"))) Expect(detachTagOptionsModel.TagNames).To(Equal([]string{"testString"})) + Expect(detachTagOptionsModel.AccountID).To(Equal(core.StringPtr("testString"))) + Expect(detachTagOptionsModel.TagType).To(Equal(core.StringPtr("user"))) Expect(detachTagOptionsModel.Headers).To(Equal(map[string]string{"foo": "bar"})) }) It(`Invoke NewListTagsOptions successfully`, func() { // Construct an instance of the ListTagsOptions model listTagsOptionsModel := globalTaggingService.NewListTagsOptions() + listTagsOptionsModel.SetAccountID("testString") + listTagsOptionsModel.SetTagType("user") + listTagsOptionsModel.SetFullData(true) listTagsOptionsModel.SetProviders([]string{"ghost"}) listTagsOptionsModel.SetAttachedTo("testString") - listTagsOptionsModel.SetFullData(true) listTagsOptionsModel.SetOffset(int64(0)) listTagsOptionsModel.SetLimit(int64(1)) + listTagsOptionsModel.SetTimeout(int64(0)) listTagsOptionsModel.SetOrderByName("asc") - listTagsOptionsModel.SetTimeout(int64(38)) listTagsOptionsModel.SetAttachedOnly(true) listTagsOptionsModel.SetHeaders(map[string]string{"foo": "bar"}) Expect(listTagsOptionsModel).ToNot(BeNil()) + Expect(listTagsOptionsModel.AccountID).To(Equal(core.StringPtr("testString"))) + Expect(listTagsOptionsModel.TagType).To(Equal(core.StringPtr("user"))) + Expect(listTagsOptionsModel.FullData).To(Equal(core.BoolPtr(true))) Expect(listTagsOptionsModel.Providers).To(Equal([]string{"ghost"})) Expect(listTagsOptionsModel.AttachedTo).To(Equal(core.StringPtr("testString"))) - Expect(listTagsOptionsModel.FullData).To(Equal(core.BoolPtr(true))) Expect(listTagsOptionsModel.Offset).To(Equal(core.Int64Ptr(int64(0)))) Expect(listTagsOptionsModel.Limit).To(Equal(core.Int64Ptr(int64(1)))) + Expect(listTagsOptionsModel.Timeout).To(Equal(core.Int64Ptr(int64(0)))) Expect(listTagsOptionsModel.OrderByName).To(Equal(core.StringPtr("asc"))) - Expect(listTagsOptionsModel.Timeout).To(Equal(core.Int64Ptr(int64(38)))) Expect(listTagsOptionsModel.AttachedOnly).To(Equal(core.BoolPtr(true))) Expect(listTagsOptionsModel.Headers).To(Equal(map[string]string{"foo": "bar"})) }) @@ -882,6 +1116,31 @@ var _ = Describe(`GlobalTaggingV1`, func() { Expect(model).ToNot(BeNil()) Expect(err).To(BeNil()) }) + It(`Invoke NewSetTagsOptions successfully`, func() { + // Construct an instance of the Resource model + resourceModel := new(globaltaggingv1.Resource) + Expect(resourceModel).ToNot(BeNil()) + resourceModel.ResourceID = core.StringPtr("testString") + resourceModel.ResourceType = core.StringPtr("testString") + Expect(resourceModel.ResourceID).To(Equal(core.StringPtr("testString"))) + Expect(resourceModel.ResourceType).To(Equal(core.StringPtr("testString"))) + + // Construct an instance of the SetTagsOptions model + setTagsOptionsTagNames := []string{"testString"} + setTagsOptionsResources := []globaltaggingv1.Resource{} + setTagsOptionsModel := globalTaggingService.NewSetTagsOptions(setTagsOptionsTagNames, setTagsOptionsResources) + setTagsOptionsModel.SetTagNames([]string{"testString"}) + setTagsOptionsModel.SetResources([]globaltaggingv1.Resource{*resourceModel}) + setTagsOptionsModel.SetXGhost("testString") + setTagsOptionsModel.SetAccountID("testString") + setTagsOptionsModel.SetHeaders(map[string]string{"foo": "bar"}) + Expect(setTagsOptionsModel).ToNot(BeNil()) + Expect(setTagsOptionsModel.TagNames).To(Equal([]string{"testString"})) + Expect(setTagsOptionsModel.Resources).To(Equal([]globaltaggingv1.Resource{*resourceModel})) + Expect(setTagsOptionsModel.XGhost).To(Equal(core.StringPtr("testString"))) + Expect(setTagsOptionsModel.AccountID).To(Equal(core.StringPtr("testString"))) + Expect(setTagsOptionsModel.Headers).To(Equal(map[string]string{"foo": "bar"})) + }) }) }) Describe(`Utility function tests`, func() { From fa1bcff18c19a60c506cd2d711a57a9f66489d48 Mon Sep 17 00:00:00 2001 From: Phil Adams Date: Thu, 24 Sep 2020 09:21:25 -0500 Subject: [PATCH 2/4] chore: re-gen service after API update; adjust tests/examples --- globaltaggingv1/global_tagging_v1.go | 145 --------------- .../global_tagging_v1_examples_test.go | 27 +-- .../global_tagging_v1_integration_test.go | 72 ++------ globaltaggingv1/global_tagging_v1_test.go | 169 ------------------ 4 files changed, 18 insertions(+), 395 deletions(-) diff --git a/globaltaggingv1/global_tagging_v1.go b/globaltaggingv1/global_tagging_v1.go index ddc50e40..a0c82f81 100644 --- a/globaltaggingv1/global_tagging_v1.go +++ b/globaltaggingv1/global_tagging_v1.go @@ -452,76 +452,6 @@ func (globalTagging *GlobalTaggingV1) DetachTag(detachTagOptions *DetachTagOptio return } -// SetTags : Set tags for a resource -// Tag one resource by passing in one or more tags. Tag references are cleared out every time this operation is called. -// If your resource is already tagged you will need to pass the current tags along with any new ones. To remove all tag -// references pass an empty tag_names array. To remove one or more tags omit them from the tag_names array. You can only -// set tags to a specific subset of IMS resources and only if you have the needed permission. -func (globalTagging *GlobalTaggingV1) SetTags(setTagsOptions *SetTagsOptions) (result *SetTagsResults, response *core.DetailedResponse, err error) { - err = core.ValidateNotNil(setTagsOptions, "setTagsOptions cannot be nil") - if err != nil { - return - } - err = core.ValidateStruct(setTagsOptions, "setTagsOptions") - if err != nil { - return - } - - builder := core.NewRequestBuilder(core.POST) - _, err = builder.ResolveRequestURL(globalTagging.Service.Options.URL, `/v3/tags/set_tags`, nil) - if err != nil { - return - } - - for headerName, headerValue := range setTagsOptions.Headers { - builder.AddHeader(headerName, headerValue) - } - - sdkHeaders := common.GetSdkHeaders("global_tagging", "V1", "SetTags") - for headerName, headerValue := range sdkHeaders { - builder.AddHeader(headerName, headerValue) - } - builder.AddHeader("Accept", "application/json") - builder.AddHeader("Content-Type", "application/json") - if setTagsOptions.XGhost != nil { - builder.AddHeader("X-Ghost", fmt.Sprint(*setTagsOptions.XGhost)) - } - - if setTagsOptions.AccountID != nil { - builder.AddQuery("account_id", fmt.Sprint(*setTagsOptions.AccountID)) - } - - body := make(map[string]interface{}) - if setTagsOptions.TagNames != nil { - body["tag_names"] = setTagsOptions.TagNames - } - if setTagsOptions.Resources != nil { - body["resources"] = setTagsOptions.Resources - } - _, err = builder.SetBodyContentJSON(body) - if err != nil { - return - } - - request, err := builder.Build() - if err != nil { - return - } - - var rawResponse map[string]json.RawMessage - response, err = globalTagging.Service.Request(request, &rawResponse) - if err != nil { - return - } - err = core.UnmarshalModel(rawResponse, "", &result, UnmarshalSetTagsResults) - if err != nil { - return - } - response.Result = result - - return -} - // AttachTagOptions : The AttachTag options. type AttachTagOptions struct { // List of resources on which the tag or tags should be attached. @@ -1124,81 +1054,6 @@ func UnmarshalResource(m map[string]json.RawMessage, result interface{}) (err er return } -// SetTagsOptions : The SetTags options. -type SetTagsOptions struct { - // An array of tag names to set. - TagNames []string `json:"tag_names" validate:"required"` - - // List of resources on which the tags should be set. Current implementation support only one resource whose type is - // included in a specific subset of IMS resources. - Resources []Resource `json:"resources" validate:"required"` - - // GhoST reserved. - XGhost *string `json:"X-Ghost,omitempty"` - - // The ID of the billing account where the resources to be tagged lives. - AccountID *string `json:"account_id,omitempty"` - - // Allows users to set headers on API requests - Headers map[string]string -} - -// NewSetTagsOptions : Instantiate SetTagsOptions -func (*GlobalTaggingV1) NewSetTagsOptions(tagNames []string, resources []Resource) *SetTagsOptions { - return &SetTagsOptions{ - TagNames: tagNames, - Resources: resources, - } -} - -// SetTagNames : Allow user to set TagNames -func (options *SetTagsOptions) SetTagNames(tagNames []string) *SetTagsOptions { - options.TagNames = tagNames - return options -} - -// SetResources : Allow user to set Resources -func (options *SetTagsOptions) SetResources(resources []Resource) *SetTagsOptions { - options.Resources = resources - return options -} - -// SetXGhost : Allow user to set XGhost -func (options *SetTagsOptions) SetXGhost(xGhost string) *SetTagsOptions { - options.XGhost = core.StringPtr(xGhost) - return options -} - -// SetAccountID : Allow user to set AccountID -func (options *SetTagsOptions) SetAccountID(accountID string) *SetTagsOptions { - options.AccountID = core.StringPtr(accountID) - return options -} - -// SetHeaders : Allow user to set Headers -func (options *SetTagsOptions) SetHeaders(param map[string]string) *SetTagsOptions { - options.Headers = param - return options -} - -// SetTagsResults : Results of a set_tags request. -type SetTagsResults struct { - // Array of results of an set_tags request. - Results []TagResultsItem `json:"results,omitempty"` -} - - -// UnmarshalSetTagsResults unmarshals an instance of SetTagsResults from the specified map of raw messages. -func UnmarshalSetTagsResults(m map[string]json.RawMessage, result interface{}) (err error) { - obj := new(SetTagsResults) - err = core.UnmarshalModel(m, "results", &obj.Results, UnmarshalTagResultsItem) - if err != nil { - return - } - reflect.ValueOf(result).Elem().Set(reflect.ValueOf(obj)) - return -} - // Tag : A tag. type Tag struct { // This is the name of the tag. diff --git a/globaltaggingv1/global_tagging_v1_examples_test.go b/globaltaggingv1/global_tagging_v1_examples_test.go index 542bb165..679967e5 100644 --- a/globaltaggingv1/global_tagging_v1_examples_test.go +++ b/globaltaggingv1/global_tagging_v1_examples_test.go @@ -109,6 +109,7 @@ var _ = Describe(`GlobalTaggingV1 Examples Tests`, func() { // begin-list_tags listTagsOptions := globalTaggingService.NewListTagsOptions() + listTagsOptions.SetAttachedOnly(true) tagList, response, err := globalTaggingService.ListTags(listTagsOptions) if err != nil { @@ -175,32 +176,6 @@ var _ = Describe(`GlobalTaggingV1 Examples Tests`, func() { Expect(response.StatusCode).To(Equal(200)) Expect(tagResults).ToNot(BeNil()) - }) - It(`SetTags request example`, func() { - // begin-set_tags - - resourceModel := &globaltaggingv1.Resource{ - ResourceID: &resourceCRN, - } - - setTagsOptions := globalTaggingService.NewSetTagsOptions( - []string{"tag_test_1", "tag_test_2"}, - []globaltaggingv1.Resource{*resourceModel}, - ) - - setTagsResults, response, err := globalTaggingService.SetTags(setTagsOptions) - if err != nil { - panic(err) - } - b, _ := json.MarshalIndent(setTagsResults, "", " ") - fmt.Printf("\nSetTags() result:\n%s\n", string(b)) - - // end-set_tags - - Expect(err).To(BeNil()) - Expect(response.StatusCode).To(Equal(200)) - Expect(setTagsResults).ToNot(BeNil()) - }) It(`DeleteTagAll request example`, func() { // begin-delete_tag_all diff --git a/globaltaggingv1/global_tagging_v1_integration_test.go b/globaltaggingv1/global_tagging_v1_integration_test.go index fa6d324b..18794e8d 100644 --- a/globaltaggingv1/global_tagging_v1_integration_test.go +++ b/globaltaggingv1/global_tagging_v1_integration_test.go @@ -211,61 +211,23 @@ var _ = Describe(`GlobalTaggingV1 Integration Tests`, func() { }) }) - // Describe(`SetTags - Set tags for a resource`, func() { - // BeforeEach(func() { - // shouldSkipTest() - // }) - // It(`SetTags(setTagsOptions *SetTagsOptions)`, func() { - - // resourceModel := &globaltaggingv1.Resource{ - // ResourceID: &crn, - // } - - // setTagsOptions := &globaltaggingv1.SetTagsOptions{ - // TagNames: []string{tagName}, - // Resources: []globaltaggingv1.Resource{*resourceModel}, - // } - - // setTagsResults, response, err := globalTaggingService.SetTags(setTagsOptions) - - // Expect(err).To(BeNil()) - // Expect(response.StatusCode).To(Equal(200)) - // Expect(setTagsResults).ToNot(BeNil()) - // fmt.Printf("\nSetTags() response:\n%s", toJson(setTagsResults)) - // Expect(setTagsResults.Results).ToNot(BeEmpty()) - // for _, elem := range setTagsResults.Results { - // Expect(*elem.IsError).To(Equal(false)) - // } - - // // Make sure we end up with only the one tag set on the resource. - // tagNames := getTagNamesForResource(globalTaggingService, crn) - // fmt.Print("\nResource now has these tags: ", tagNames) - // Expect(tagNames).ToNot(BeEmpty()) - // Expect(len(tagNames)).To(Equal(1)) - // Expect(tagNames).To(ContainElement(tagName)) - // }) - // }) - - // Describe(`DeleteTagAll - Delete all unused tags`, func() { - // BeforeEach(func() { - // shouldSkipTest() - // }) - // It(`DeleteTagAll(deleteTagAllOptions *DeleteTagAllOptions)`, func() { - - // deleteTagAllOptions := &globaltaggingv1.DeleteTagAllOptions{ - // Providers: core.StringPtr("ghost"), - // AccountID: core.StringPtr("testString"), - // TagType: core.StringPtr("user"), - // } - - // deleteTagsResult, response, err := globalTaggingService.DeleteTagAll(deleteTagAllOptions) - - // Expect(err).To(BeNil()) - // Expect(response.StatusCode).To(Equal(200)) - // Expect(deleteTagsResult).ToNot(BeNil()) - - // }) - // }) + Describe(`DeleteTagAll - Delete all unused tags`, func() { + BeforeEach(func() { + shouldSkipTest() + }) + It(`DeleteTagAll(deleteTagAllOptions *DeleteTagAllOptions)`, func() { + + deleteTagAllOptions := &globaltaggingv1.DeleteTagAllOptions{} + + deleteTagsResult, response, err := globalTaggingService.DeleteTagAll(deleteTagAllOptions) + + Expect(err).To(BeNil()) + Expect(response.StatusCode).To(Equal(200)) + Expect(deleteTagsResult).ToNot(BeNil()) + // fmt.Printf("\nDetachTagAll() response:\n%s", toJson(deleteTagsResult)) + // fmt.Printf("\nDeleteTagAll deleted %d unused tags.\n", *deleteTagsResult.TotalCount) + }) + }) }) func getTagNamesForResource(service *globaltaggingv1.GlobalTaggingV1, resourceID string) []string { diff --git a/globaltaggingv1/global_tagging_v1_test.go b/globaltaggingv1/global_tagging_v1_test.go index 85426d25..9d1e8d22 100644 --- a/globaltaggingv1/global_tagging_v1_test.go +++ b/globaltaggingv1/global_tagging_v1_test.go @@ -852,150 +852,6 @@ var _ = Describe(`GlobalTaggingV1`, func() { }) }) }) - Describe(`SetTags(setTagsOptions *SetTagsOptions) - Operation response error`, func() { - setTagsPath := "/v3/tags/set_tags" - Context(`Using mock server endpoint`, func() { - BeforeEach(func() { - testServer = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { - defer GinkgoRecover() - - // Verify the contents of the request - Expect(req.URL.EscapedPath()).To(Equal(setTagsPath)) - Expect(req.Method).To(Equal("POST")) - Expect(req.Header["X-Ghost"]).ToNot(BeNil()) - Expect(req.Header["X-Ghost"][0]).To(Equal(fmt.Sprintf("%v", "testString"))) - Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) - - res.Header().Set("Content-type", "application/json") - res.WriteHeader(200) - fmt.Fprintf(res, `} this is not valid json {`) - })) - }) - It(`Invoke SetTags with error: Operation response processing error`, func() { - globalTaggingService, serviceErr := globaltaggingv1.NewGlobalTaggingV1(&globaltaggingv1.GlobalTaggingV1Options{ - URL: testServer.URL, - Authenticator: &core.NoAuthAuthenticator{}, - }) - Expect(serviceErr).To(BeNil()) - Expect(globalTaggingService).ToNot(BeNil()) - - // Construct an instance of the Resource model - resourceModel := new(globaltaggingv1.Resource) - resourceModel.ResourceID = core.StringPtr("testString") - resourceModel.ResourceType = core.StringPtr("testString") - - // Construct an instance of the SetTagsOptions model - setTagsOptionsModel := new(globaltaggingv1.SetTagsOptions) - setTagsOptionsModel.TagNames = []string{"testString"} - setTagsOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} - setTagsOptionsModel.XGhost = core.StringPtr("testString") - setTagsOptionsModel.AccountID = core.StringPtr("testString") - setTagsOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} - // Expect response parsing to fail since we are receiving a text/plain response - result, response, operationErr := globalTaggingService.SetTags(setTagsOptionsModel) - Expect(operationErr).ToNot(BeNil()) - Expect(response).ToNot(BeNil()) - Expect(result).To(BeNil()) - }) - AfterEach(func() { - testServer.Close() - }) - }) - }) - - Describe(`SetTags(setTagsOptions *SetTagsOptions)`, func() { - setTagsPath := "/v3/tags/set_tags" - Context(`Using mock server endpoint`, func() { - BeforeEach(func() { - testServer = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { - defer GinkgoRecover() - - // Verify the contents of the request - Expect(req.URL.EscapedPath()).To(Equal(setTagsPath)) - Expect(req.Method).To(Equal("POST")) - Expect(req.Header["X-Ghost"]).ToNot(BeNil()) - Expect(req.Header["X-Ghost"][0]).To(Equal(fmt.Sprintf("%v", "testString"))) - Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"})) - - res.Header().Set("Content-type", "application/json") - res.WriteHeader(200) - fmt.Fprintf(res, "%s", `{"results": [{"resource_id": "ResourceID", "is_error": false}]}`) - })) - }) - It(`Invoke SetTags successfully`, func() { - globalTaggingService, serviceErr := globaltaggingv1.NewGlobalTaggingV1(&globaltaggingv1.GlobalTaggingV1Options{ - URL: testServer.URL, - Authenticator: &core.NoAuthAuthenticator{}, - }) - Expect(serviceErr).To(BeNil()) - Expect(globalTaggingService).ToNot(BeNil()) - - // Invoke operation with nil options model (negative test) - result, response, operationErr := globalTaggingService.SetTags(nil) - Expect(operationErr).NotTo(BeNil()) - Expect(response).To(BeNil()) - Expect(result).To(BeNil()) - - // Construct an instance of the Resource model - resourceModel := new(globaltaggingv1.Resource) - resourceModel.ResourceID = core.StringPtr("testString") - resourceModel.ResourceType = core.StringPtr("testString") - - // Construct an instance of the SetTagsOptions model - setTagsOptionsModel := new(globaltaggingv1.SetTagsOptions) - setTagsOptionsModel.TagNames = []string{"testString"} - setTagsOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} - setTagsOptionsModel.XGhost = core.StringPtr("testString") - setTagsOptionsModel.AccountID = core.StringPtr("testString") - setTagsOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} - - // Invoke operation with valid options model (positive test) - result, response, operationErr = globalTaggingService.SetTags(setTagsOptionsModel) - Expect(operationErr).To(BeNil()) - Expect(response).ToNot(BeNil()) - Expect(result).ToNot(BeNil()) - }) - It(`Invoke SetTags with error: Operation validation and request error`, func() { - globalTaggingService, serviceErr := globaltaggingv1.NewGlobalTaggingV1(&globaltaggingv1.GlobalTaggingV1Options{ - URL: testServer.URL, - Authenticator: &core.NoAuthAuthenticator{}, - }) - Expect(serviceErr).To(BeNil()) - Expect(globalTaggingService).ToNot(BeNil()) - - // Construct an instance of the Resource model - resourceModel := new(globaltaggingv1.Resource) - resourceModel.ResourceID = core.StringPtr("testString") - resourceModel.ResourceType = core.StringPtr("testString") - - // Construct an instance of the SetTagsOptions model - setTagsOptionsModel := new(globaltaggingv1.SetTagsOptions) - setTagsOptionsModel.TagNames = []string{"testString"} - setTagsOptionsModel.Resources = []globaltaggingv1.Resource{*resourceModel} - setTagsOptionsModel.XGhost = core.StringPtr("testString") - setTagsOptionsModel.AccountID = core.StringPtr("testString") - setTagsOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"} - // Invoke operation with empty URL (negative test) - err := globalTaggingService.SetServiceURL("") - Expect(err).To(BeNil()) - result, response, operationErr := globalTaggingService.SetTags(setTagsOptionsModel) - Expect(operationErr).ToNot(BeNil()) - Expect(operationErr.Error()).To(ContainSubstring(core.ERRORMSG_SERVICE_URL_MISSING)) - Expect(response).To(BeNil()) - Expect(result).To(BeNil()) - // Construct a second instance of the SetTagsOptions model with no property values - setTagsOptionsModelNew := new(globaltaggingv1.SetTagsOptions) - // Invoke operation with invalid model (negative test) - result, response, operationErr = globalTaggingService.SetTags(setTagsOptionsModelNew) - Expect(operationErr).ToNot(BeNil()) - Expect(response).To(BeNil()) - Expect(result).To(BeNil()) - }) - AfterEach(func() { - testServer.Close() - }) - }) - }) Describe(`Model constructor tests`, func() { Context(`Using a service client instance`, func() { globalTaggingService, _ := globaltaggingv1.NewGlobalTaggingV1(&globaltaggingv1.GlobalTaggingV1Options{ @@ -1116,31 +972,6 @@ var _ = Describe(`GlobalTaggingV1`, func() { Expect(model).ToNot(BeNil()) Expect(err).To(BeNil()) }) - It(`Invoke NewSetTagsOptions successfully`, func() { - // Construct an instance of the Resource model - resourceModel := new(globaltaggingv1.Resource) - Expect(resourceModel).ToNot(BeNil()) - resourceModel.ResourceID = core.StringPtr("testString") - resourceModel.ResourceType = core.StringPtr("testString") - Expect(resourceModel.ResourceID).To(Equal(core.StringPtr("testString"))) - Expect(resourceModel.ResourceType).To(Equal(core.StringPtr("testString"))) - - // Construct an instance of the SetTagsOptions model - setTagsOptionsTagNames := []string{"testString"} - setTagsOptionsResources := []globaltaggingv1.Resource{} - setTagsOptionsModel := globalTaggingService.NewSetTagsOptions(setTagsOptionsTagNames, setTagsOptionsResources) - setTagsOptionsModel.SetTagNames([]string{"testString"}) - setTagsOptionsModel.SetResources([]globaltaggingv1.Resource{*resourceModel}) - setTagsOptionsModel.SetXGhost("testString") - setTagsOptionsModel.SetAccountID("testString") - setTagsOptionsModel.SetHeaders(map[string]string{"foo": "bar"}) - Expect(setTagsOptionsModel).ToNot(BeNil()) - Expect(setTagsOptionsModel.TagNames).To(Equal([]string{"testString"})) - Expect(setTagsOptionsModel.Resources).To(Equal([]globaltaggingv1.Resource{*resourceModel})) - Expect(setTagsOptionsModel.XGhost).To(Equal(core.StringPtr("testString"))) - Expect(setTagsOptionsModel.AccountID).To(Equal(core.StringPtr("testString"))) - Expect(setTagsOptionsModel.Headers).To(Equal(map[string]string{"foo": "bar"})) - }) }) }) Describe(`Utility function tests`, func() { From e25d66d5c0a6342ac7c586aed5b4be1e2b1e9173 Mon Sep 17 00:00:00 2001 From: Phil Adams Date: Thu, 24 Sep 2020 11:46:05 -0500 Subject: [PATCH 3/4] test: re-order methods in examples --- .../global_tagging_v1_examples_test.go | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/globaltaggingv1/global_tagging_v1_examples_test.go b/globaltaggingv1/global_tagging_v1_examples_test.go index 679967e5..dbb3f9f5 100644 --- a/globaltaggingv1/global_tagging_v1_examples_test.go +++ b/globaltaggingv1/global_tagging_v1_examples_test.go @@ -177,44 +177,42 @@ var _ = Describe(`GlobalTaggingV1 Examples Tests`, func() { Expect(tagResults).ToNot(BeNil()) }) - It(`DeleteTagAll request example`, func() { - // begin-delete_tag_all + It(`DeleteTag request example`, func() { + // begin-delete_tag - deleteTagAllOptions := globalTaggingService.NewDeleteTagAllOptions() + deleteTagOptions := globalTaggingService.NewDeleteTagOptions("tag_test_1") - deleteTagsResult, response, err := globalTaggingService.DeleteTagAll(deleteTagAllOptions) + deleteTagResults, response, err := globalTaggingService.DeleteTag(deleteTagOptions) if err != nil { panic(err) } - b, _ := json.MarshalIndent(deleteTagsResult, "", " ") - fmt.Printf("\nDeleteTagAll() result:\n%s\n", string(b)) + b, _ := json.MarshalIndent(deleteTagResults, "", " ") + fmt.Printf("\nDeleteTag() result:\n%s\n", string(b)) - // end-delete_tag_all + // end-delete_tag Expect(err).To(BeNil()) Expect(response.StatusCode).To(Equal(200)) - Expect(deleteTagsResult).ToNot(BeNil()) + Expect(deleteTagResults).ToNot(BeNil()) }) - It(`DeleteTag request example`, func() { - // begin-delete_tag + It(`DeleteTagAll request example`, func() { + // begin-delete_tag_all - deleteTagOptions := globalTaggingService.NewDeleteTagOptions( - "tag_test_1", - ) + deleteTagAllOptions := globalTaggingService.NewDeleteTagAllOptions() - deleteTagResults, response, err := globalTaggingService.DeleteTag(deleteTagOptions) + deleteTagsResult, response, err := globalTaggingService.DeleteTagAll(deleteTagAllOptions) if err != nil { panic(err) } - b, _ := json.MarshalIndent(deleteTagResults, "", " ") - fmt.Printf("\nDeleteTag() result:\n%s\n", string(b)) + b, _ := json.MarshalIndent(deleteTagsResult, "", " ") + fmt.Printf("\nDeleteTagAll() result:\n%s\n", string(b)) - // end-delete_tag + // end-delete_tag_all Expect(err).To(BeNil()) Expect(response.StatusCode).To(Equal(200)) - Expect(deleteTagResults).ToNot(BeNil()) + Expect(deleteTagsResult).ToNot(BeNil()) }) }) From 10042dfebc6c00d06495f08128487fbf9059b5eb Mon Sep 17 00:00:00 2001 From: Phil Adams Date: Tue, 29 Sep 2020 12:24:25 -0500 Subject: [PATCH 4/4] test: make examples consistent across SDKs --- globaltaggingv1/global_tagging_v1_examples_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/globaltaggingv1/global_tagging_v1_examples_test.go b/globaltaggingv1/global_tagging_v1_examples_test.go index dbb3f9f5..3d6a4f35 100644 --- a/globaltaggingv1/global_tagging_v1_examples_test.go +++ b/globaltaggingv1/global_tagging_v1_examples_test.go @@ -116,7 +116,7 @@ var _ = Describe(`GlobalTaggingV1 Examples Tests`, func() { panic(err) } b, _ := json.MarshalIndent(tagList, "", " ") - fmt.Printf("\nListTags() result:\n%s\n", string(b)) + fmt.Println(string(b)) // end-list_tags @@ -142,7 +142,7 @@ var _ = Describe(`GlobalTaggingV1 Examples Tests`, func() { panic(err) } b, _ := json.MarshalIndent(tagResults, "", " ") - fmt.Printf("\nAttachTag() result:\n%s\n", string(b)) + fmt.Println(string(b)) // end-attach_tag @@ -168,7 +168,7 @@ var _ = Describe(`GlobalTaggingV1 Examples Tests`, func() { panic(err) } b, _ := json.MarshalIndent(tagResults, "", " ") - fmt.Printf("\nDetachTag() result:\n%s\n", string(b)) + fmt.Println(string(b)) // end-detach_tag @@ -187,7 +187,7 @@ var _ = Describe(`GlobalTaggingV1 Examples Tests`, func() { panic(err) } b, _ := json.MarshalIndent(deleteTagResults, "", " ") - fmt.Printf("\nDeleteTag() result:\n%s\n", string(b)) + fmt.Println(string(b)) // end-delete_tag @@ -206,7 +206,7 @@ var _ = Describe(`GlobalTaggingV1 Examples Tests`, func() { panic(err) } b, _ := json.MarshalIndent(deleteTagsResult, "", " ") - fmt.Printf("\nDeleteTagAll() result:\n%s\n", string(b)) + fmt.Println(string(b)) // end-delete_tag_all