|
| 1 | +# coding: utf-8 |
| 2 | +# Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
| 3 | + |
| 4 | +# This example demonstrates how to programatically retrieve resource types and |
| 5 | +# query for resources. |
| 6 | +# |
| 7 | +# The search service documentation can be found here: |
| 8 | +# https://docs.cloud.oracle.com/iaas/Content/Search/Concepts/queryoverview.htm |
| 9 | +# |
| 10 | +# When running this example, it is assumed that you already have active users |
| 11 | +# resources associated with your tenant to display the search results. |
| 12 | +# |
| 13 | +# Querying for resources may be done via a structured query or free text. |
| 14 | +# For more information on how to format queries, please refer to |
| 15 | +# https://docs.cloud.oracle.com/iaas/Content/Search/Concepts/querysyntax.htm |
| 16 | + |
| 17 | +from __future__ import print_function |
| 18 | +import oci |
| 19 | + |
| 20 | + |
| 21 | +# Load the default configuration |
| 22 | +config = oci.config.from_file() |
| 23 | + |
| 24 | +# This is the root compartment. You can use another compartment in your tenancy. |
| 25 | +compartment_id = config["tenancy"] |
| 26 | + |
| 27 | +search_client = oci.resource_search.ResourceSearchClient(config) |
| 28 | + |
| 29 | + |
| 30 | +def resource_types(): |
| 31 | + # Resource types |
| 32 | + # This is will produce a printed list of the resource types and fields |
| 33 | + # There are more details available than what is displayed. |
| 34 | + print() |
| 35 | + print("Resources and their fields") |
| 36 | + print("==========================") |
| 37 | + response = search_client.list_resource_types() |
| 38 | + for resource_type in response.data: |
| 39 | + print(resource_type.name) |
| 40 | + for field in resource_type.fields: |
| 41 | + print("\t {}".format(field.field_name)) |
| 42 | + |
| 43 | + |
| 44 | +def fields_in_instance_resource(): |
| 45 | + # A more detailed look at the freeformTags field in the Instance resource |
| 46 | + print() |
| 47 | + print("Instance resource, freeformTags field") |
| 48 | + print("=====================================") |
| 49 | + response = search_client.list_resource_types() |
| 50 | + for resource_type in response.data: |
| 51 | + if resource_type.name == "Instance": |
| 52 | + instance = resource_type |
| 53 | + for field in instance.fields: |
| 54 | + print(field) |
| 55 | + |
| 56 | + |
| 57 | +def field_names_in_instance_resource(): |
| 58 | + # The details for a single resource type can be retrieved without |
| 59 | + # retrieving all the resources. |
| 60 | + print() |
| 61 | + print("Get a single resource type (Instance) from Search service") |
| 62 | + print("=========================================================") |
| 63 | + instance = search_client.get_resource_type('Instance').data |
| 64 | + fields = [x.field_name for x in instance.fields] |
| 65 | + print(fields) |
| 66 | + |
| 67 | + |
| 68 | +def active_users(): |
| 69 | + # Get all users which do not have the inactiveStatus set. |
| 70 | + # This is the same as "query user resources where lifecycleState = 'ACTIVE'" |
| 71 | + print() |
| 72 | + print("Get users which are active, using StructuredSearchDetails") |
| 73 | + print("=========================================================") |
| 74 | + structured_search = oci.resource_search.models.StructuredSearchDetails(query="query user resources where inactiveStatus = 0", |
| 75 | + type='Structured', |
| 76 | + matching_context_type=oci.resource_search.models.SearchDetails.MATCHING_CONTEXT_TYPE_NONE) |
| 77 | + users = search_client.search_resources(structured_search) |
| 78 | + |
| 79 | + for user in users.data.items: |
| 80 | + print(user.display_name) |
| 81 | + |
| 82 | + |
| 83 | +def search_with_free_text(): |
| 84 | + # Get all resources whose lifecycleState is AVAILABLE |
| 85 | + # Using pagination |
| 86 | + print("Get resources which are available, using FreeTextSearchDetails") |
| 87 | + print("==============================================================") |
| 88 | + |
| 89 | + free_text_search = oci.resource_search.models.FreeTextSearchDetails(text="lifecycleState as AVAILABLE", |
| 90 | + type='FreeText', |
| 91 | + matching_context_type=oci.resource_search.models.SearchDetails.MATCHING_CONTEXT_TYPE_HIGHLIGHTS) |
| 92 | + |
| 93 | + for response in oci.pagination.list_call_get_all_results_generator(search_client.search_resources, 'response', free_text_search): |
| 94 | + for resource in response.data.items: |
| 95 | + print("Resource type: {}, Resource name: {}".format(resource.resource_type, resource.display_name)) |
| 96 | + |
| 97 | + |
| 98 | +def users_by_freeformTag(tag): |
| 99 | + # Search for user resources with a freeform tag. |
| 100 | + print() |
| 101 | + print("Get users based on having a freeformTag") |
| 102 | + print("=======================================") |
| 103 | + structured_search = oci.resource_search.models.StructuredSearchDetails(query="query user resources where freeformTags.key = '{}'".format(tag), |
| 104 | + type='Structured', |
| 105 | + matching_context_type=oci.resource_search.models.SearchDetails.MATCHING_CONTEXT_TYPE_NONE) |
| 106 | + users = search_client.search_resources(structured_search) |
| 107 | + |
| 108 | + for user in users.data.items: |
| 109 | + print(user) |
| 110 | + |
| 111 | + |
| 112 | +def users_by_freeformTag_and_value(tag, value): |
| 113 | + # Search for users with a freeform tag set to a particular value. |
| 114 | + print() |
| 115 | + print("Get users based on having a freeformTag which matches a value") |
| 116 | + print("=============================================================") |
| 117 | + structured_search = oci.resource_search.models.StructuredSearchDetails(query="query user resources where (freeformTags.key = '{}' && freeformTags.value = '{}')".format(tag, value), |
| 118 | + type='Structured', |
| 119 | + matching_context_type=oci.resource_search.models.SearchDetails.MATCHING_CONTEXT_TYPE_NONE) |
| 120 | + users = search_client.search_resources(structured_search) |
| 121 | + |
| 122 | + for user in users.data.items: |
| 123 | + print(user) |
| 124 | + |
| 125 | + |
| 126 | +# Run examples |
| 127 | +resource_types() |
| 128 | +fields_in_instance_resource() |
| 129 | +field_names_in_instance_resource() |
| 130 | +search_with_free_text() |
| 131 | +active_users() |
| 132 | + |
| 133 | +# These next examples need to be customized to your situation. |
| 134 | +# Let's assume you added a freeform tag "role" to some of your users and |
| 135 | +# one of the values is "developer". Then you could replace the |
| 136 | +# <your_tag_here> with role and <your_value_here> with developer. |
| 137 | +# These examples will then retrieve every user that had the role freeform tag |
| 138 | +# and the role freeform tag where the value is set to developer. |
| 139 | +# users_by_freeformTag("<your_tag_here>") |
| 140 | +# users_by_freeformTag_and_value("<your_tag_here>", "<your_value_here>") |
0 commit comments