Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions modules/examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<artifactId>configuration-governance</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>global-tagging</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* (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 com.ibm.cloud.platform_services.global_tagging.v1;

import com.ibm.cloud.platform_services.global_tagging.v1.model.AttachTagOptions;
import com.ibm.cloud.platform_services.global_tagging.v1.model.DeleteTagAllOptions;
import com.ibm.cloud.platform_services.global_tagging.v1.model.DeleteTagOptions;
import com.ibm.cloud.platform_services.global_tagging.v1.model.DeleteTagResults;
import com.ibm.cloud.platform_services.global_tagging.v1.model.DeleteTagsResult;
import com.ibm.cloud.platform_services.global_tagging.v1.model.DetachTagOptions;
import com.ibm.cloud.platform_services.global_tagging.v1.model.ListTagsOptions;
import com.ibm.cloud.platform_services.global_tagging.v1.model.Resource;
import com.ibm.cloud.platform_services.global_tagging.v1.model.TagList;
import com.ibm.cloud.platform_services.global_tagging.v1.model.TagResults;
import com.ibm.cloud.sdk.core.http.Response;
import com.ibm.cloud.sdk.core.service.exception.ServiceResponseException;
import com.ibm.cloud.sdk.core.util.CredentialUtils;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//
// 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=<service url>
// GLOBAL_TAGGING_AUTHTYPE=iam
// GLOBAL_TAGGING_APIKEY=<IAM api key>
// GLOBAL_TAGGING_AUTH_URL=<IAM token service URL - omit this if using the production environment>
// GLOBAL_TAGGING_RESOURCE_CRN=<the crn of the resource to be used in the examples>
//
public class GlobalTaggingExamples {
private static final Logger logger = LoggerFactory.getLogger(GlobalTaggingExamples.class);

protected GlobalTaggingExamples() {
}

static {
System.setProperty("IBM_CREDENTIALS_FILE", "../../global_tagging.env");
}

public static void main(String[] args) throws Exception {
GlobalTagging service = GlobalTagging.newInstance();

// Load up our test-specific config properties.
Map<String, String> config = CredentialUtils.getServiceProperties(GlobalTagging.DEFAULT_SERVICE_NAME);
String resourceCRN = config.get("RESOURCE_CRN");

try {
// begin-list_tags
ListTagsOptions listTagsOptions = new ListTagsOptions.Builder()
.attachedOnly(true)
.build();

Response<TagList> response = service.listTags(listTagsOptions).execute();
TagList tagList = response.getResult();
System.out.println(tagList.toString());
// end-list_tags
} catch (ServiceResponseException e) {
logger.error(String.format("Service returned status code %s: %s\nError details: %s", e.getStatusCode(),
e.getMessage(), e.getDebuggingInfo()), e);
}

try {
// begin-attach_tag
Resource resourceModel = new Resource.Builder()
.resourceId(resourceCRN)
.build();
AttachTagOptions attachTagOptions = new AttachTagOptions.Builder()
.addResources(resourceModel)
.addTagNames("tag_test_1")
.addTagNames("tag_test_2")
.build();

Response<TagResults> response = service.attachTag(attachTagOptions).execute();
TagResults tagResults = response.getResult();
System.out.println(tagResults.toString());
// end-attach_tag
} catch (ServiceResponseException e) {
logger.error(String.format("Service returned status code %s: %s\nError details: %s", e.getStatusCode(),
e.getMessage(), e.getDebuggingInfo()), e);
}

try {
// begin-detach_tag
Resource resourceModel = new Resource.Builder()
.resourceId(resourceCRN)
.build();
DetachTagOptions detachTagOptions = new DetachTagOptions.Builder()
.addResources(resourceModel)
.addTagNames("tag_test_1")
.addTagNames("tag_test_2")
.build();

Response<TagResults> response = service.detachTag(detachTagOptions).execute();
TagResults tagResults = response.getResult();
System.out.println(tagResults.toString());
// end-detach_tag
} catch (ServiceResponseException e) {
logger.error(String.format("Service returned status code %s: %s\nError details: %s", e.getStatusCode(),
e.getMessage(), e.getDebuggingInfo()), e);
}

try {
// begin-delete_tag
DeleteTagOptions deleteTagOptions = new DeleteTagOptions.Builder()
.tagName("tag_test_1")
.build();

Response<DeleteTagResults> response = service.deleteTag(deleteTagOptions).execute();
DeleteTagResults deleteTagResults = response.getResult();
System.out.println(deleteTagResults.toString());
// end-delete_tag
} catch (ServiceResponseException e) {
logger.error(String.format("Service returned status code %s: %s\nError details: %s", e.getStatusCode(),
e.getMessage(), e.getDebuggingInfo()), e);
}

try {
// begin-delete_tag_all
DeleteTagAllOptions deleteTagAllOptions = new DeleteTagAllOptions.Builder().build();

Response<DeleteTagsResult> response = service.deleteTagAll(deleteTagAllOptions).execute();
DeleteTagsResult deleteTagsResult = response.getResult();

System.out.println(deleteTagsResult.toString());
// end-delete_tag_all
} catch (ServiceResponseException e) {
logger.error(String.format("Service returned status code %s: %s\nError details: %s", e.getStatusCode(),
e.getMessage(), e.getDebuggingInfo()), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

/*
* IBM OpenAPI SDK Code Generator Version: 99-SNAPSHOT-cfe3553a-20200914-135527
* IBM OpenAPI SDK Code Generator Version: 99-SNAPSHOT-9b0d887a-20200923-132457
*/

package com.ibm.cloud.platform_services.global_tagging.v1;
Expand Down Expand Up @@ -43,7 +43,9 @@
/**
* 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`.
* 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 v1
*/
Expand Down Expand Up @@ -108,27 +110,33 @@ public ServiceCall<TagList> listTags(ListTagsOptions listTagsOptions) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
if (listTagsOptions.accountId() != null) {
builder.query("account_id", String.valueOf(listTagsOptions.accountId()));
}
if (listTagsOptions.tagType() != null) {
builder.query("tag_type", String.valueOf(listTagsOptions.tagType()));
}
if (listTagsOptions.fullData() != null) {
builder.query("full_data", String.valueOf(listTagsOptions.fullData()));
}
if (listTagsOptions.providers() != null) {
builder.query("providers", RequestUtils.join(listTagsOptions.providers(), ","));
}
if (listTagsOptions.attachedTo() != null) {
builder.query("attached_to", String.valueOf(listTagsOptions.attachedTo()));
}
if (listTagsOptions.fullData() != null) {
builder.query("full_data", String.valueOf(listTagsOptions.fullData()));
}
if (listTagsOptions.offset() != null) {
builder.query("offset", String.valueOf(listTagsOptions.offset()));
}
if (listTagsOptions.limit() != null) {
builder.query("limit", String.valueOf(listTagsOptions.limit()));
}
if (listTagsOptions.orderByName() != null) {
builder.query("order_by_name", String.valueOf(listTagsOptions.orderByName()));
}
if (listTagsOptions.timeout() != null) {
builder.query("timeout", String.valueOf(listTagsOptions.timeout()));
}
if (listTagsOptions.orderByName() != null) {
builder.query("order_by_name", String.valueOf(listTagsOptions.orderByName()));
}
if (listTagsOptions.attachedOnly() != null) {
builder.query("attached_only", String.valueOf(listTagsOptions.attachedOnly()));
}
Expand All @@ -150,9 +158,9 @@ public ServiceCall<TagList> listTags() {
}

/**
* Delete unused tags.
* Delete all unused tags.
*
* Delete the tags that are not attatched to any resource.
* Delete the tags that are not attached to any resource.
*
* @param deleteTagAllOptions the {@link DeleteTagAllOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link DeleteTagsResult}
Expand All @@ -170,15 +178,21 @@ public ServiceCall<DeleteTagsResult> deleteTagAll(DeleteTagAllOptions deleteTagA
if (deleteTagAllOptions.providers() != null) {
builder.query("providers", String.valueOf(deleteTagAllOptions.providers()));
}
if (deleteTagAllOptions.accountId() != null) {
builder.query("account_id", String.valueOf(deleteTagAllOptions.accountId()));
}
if (deleteTagAllOptions.tagType() != null) {
builder.query("tag_type", String.valueOf(deleteTagAllOptions.tagType()));
}
ResponseConverter<DeleteTagsResult> responseConverter =
ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DeleteTagsResult>() { }.getType());
return createServiceCall(builder.build(), responseConverter);
}

/**
* Delete unused tags.
* Delete all unused tags.
*
* Delete the tags that are not attatched to any resource.
* Delete the tags that are not attached to any resource.
*
* @return a {@link ServiceCall} with a result of type {@link DeleteTagsResult}
*/
Expand All @@ -187,7 +201,7 @@ public ServiceCall<DeleteTagsResult> deleteTagAll() {
}

/**
* Delete a tag.
* Delete an unused tag.
*
* Delete an existing tag. A tag can be deleted only if it is not attached to any resource.
*
Expand All @@ -208,18 +222,25 @@ public ServiceCall<DeleteTagResults> deleteTag(DeleteTagOptions deleteTagOptions
if (deleteTagOptions.providers() != null) {
builder.query("providers", RequestUtils.join(deleteTagOptions.providers(), ","));
}
if (deleteTagOptions.accountId() != null) {
builder.query("account_id", String.valueOf(deleteTagOptions.accountId()));
}
if (deleteTagOptions.tagType() != null) {
builder.query("tag_type", String.valueOf(deleteTagOptions.tagType()));
}
ResponseConverter<DeleteTagResults> responseConverter =
ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DeleteTagResults>() { }.getType());
return createServiceCall(builder.build(), responseConverter);
}

/**
* Attach one or more tags.
* Attach 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.
* 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.
*
* @param attachTagOptions the {@link AttachTagOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link TagResults}
Expand All @@ -233,6 +254,12 @@ public ServiceCall<TagResults> attachTag(AttachTagOptions attachTagOptions) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
if (attachTagOptions.accountId() != null) {
builder.query("account_id", String.valueOf(attachTagOptions.accountId()));
}
if (attachTagOptions.tagType() != null) {
builder.query("tag_type", String.valueOf(attachTagOptions.tagType()));
}
final JsonObject contentJson = new JsonObject();
contentJson.add("resources", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(attachTagOptions.resources()));
if (attachTagOptions.tagName() != null) {
Expand All @@ -248,13 +275,13 @@ public ServiceCall<TagResults> attachTag(AttachTagOptions attachTagOptions) {
}

/**
* Detach one or more tags.
* Detach 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.
* 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.
*
* @param detachTagOptions the {@link DetachTagOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link TagResults}
Expand All @@ -268,6 +295,12 @@ public ServiceCall<TagResults> detachTag(DetachTagOptions detachTagOptions) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
if (detachTagOptions.accountId() != null) {
builder.query("account_id", String.valueOf(detachTagOptions.accountId()));
}
if (detachTagOptions.tagType() != null) {
builder.query("tag_type", String.valueOf(detachTagOptions.tagType()));
}
final JsonObject contentJson = new JsonObject();
contentJson.add("resources", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(detachTagOptions.resources()));
if (detachTagOptions.tagName() != null) {
Expand Down
Loading