|
| 1 | +/*Copyright (c) 2023 Oracle and/or its affiliates. |
| 2 | +
|
| 3 | +The Universal Permissive License (UPL), Version 1.0 |
| 4 | +
|
| 5 | +Subject to the condition set forth below, permission is hereby granted to any |
| 6 | +person obtaining a copy of this software, associated documentation and/or data |
| 7 | +(collectively the "Software"), free of charge and under any and all copyright |
| 8 | +rights in the Software, and any and all patent rights owned or freely |
| 9 | +licensable by each licensor hereunder covering either (i) the unmodified |
| 10 | +Software as contributed to or provided by such licensor, or (ii) the Larger |
| 11 | +Works (as defined below), to deal in both |
| 12 | +
|
| 13 | +(a) the Software, and |
| 14 | +(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 15 | +one is included with the Software (each a "Larger Work" to which the Software |
| 16 | +is contributed by such licensors), |
| 17 | +
|
| 18 | +without restriction, including without limitation the rights to copy, create |
| 19 | +derivative works of, display, perform, and distribute the Software and make, |
| 20 | +use, sell, offer for sale, import, export, have made, and have sold the |
| 21 | +Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 22 | +either these or other terms. |
| 23 | +
|
| 24 | +This license is subject to the following condition: |
| 25 | +The above copyright notice and either this complete permission notice or at |
| 26 | +a minimum a reference to the UPL must be included in all copies or |
| 27 | +substantial portions of the Software. |
| 28 | +
|
| 29 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 30 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 31 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 32 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 33 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 34 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 35 | +SOFTWARE. |
| 36 | + */ |
| 37 | +package com.oracle.timg.demo.examples.identity; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | +import java.util.stream.Collectors; |
| 41 | + |
| 42 | +import com.oracle.bmc.auth.AuthenticationDetailsProvider; |
| 43 | +import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider; |
| 44 | +import com.oracle.bmc.identity.Identity; |
| 45 | +import com.oracle.bmc.identity.IdentityClient; |
| 46 | +import com.oracle.bmc.identity.model.Compartment; |
| 47 | +import com.oracle.bmc.identity.model.Compartment.LifecycleState; |
| 48 | +import com.oracle.bmc.identity.requests.ListCompartmentsRequest; |
| 49 | +import com.oracle.bmc.identity.responses.ListCompartmentsResponse; |
| 50 | +import com.oracle.bmc.streaming.StreamAdminClient; |
| 51 | +import com.oracle.bmc.streaming.requests.ListStreamsRequest; |
| 52 | +import com.oracle.bmc.streaming.responses.ListStreamsResponse; |
| 53 | + |
| 54 | +import lombok.extern.slf4j.Slf4j; |
| 55 | + |
| 56 | +//Have Lombok create a logger for us |
| 57 | +@Slf4j |
| 58 | +public class ListCompartmentsAndStreams { |
| 59 | + |
| 60 | + public static void main(String[] args) { |
| 61 | + AuthenticationDetailsProvider provider; |
| 62 | + try { |
| 63 | + provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT"); |
| 64 | + } catch (IOException e) { |
| 65 | + e.printStackTrace(); |
| 66 | + return; |
| 67 | + } |
| 68 | + StreamAdminClient streamAdminClient = new StreamAdminClient(provider); |
| 69 | + String compartmentId = provider.getTenantId(); |
| 70 | + try (Identity identityClient = new IdentityClient(provider);) { |
| 71 | + System.out.println("ListCompartments: with compartmentIdInSubtree == true"); |
| 72 | + String nextPageToken = null; |
| 73 | + do { |
| 74 | + ListCompartmentsResponse response = identityClient |
| 75 | + .listCompartments(ListCompartmentsRequest.builder().limit(50).compartmentId(compartmentId) |
| 76 | + .compartmentIdInSubtree(Boolean.TRUE).page(nextPageToken).build()); |
| 77 | + |
| 78 | + for (Compartment compartment : response.getItems()) { |
| 79 | + LifecycleState state = compartment.getLifecycleState(); |
| 80 | + if (state == LifecycleState.Active) { |
| 81 | + System.out.println(compartment); |
| 82 | + listStreamsInCompartment(compartment.getId(), streamAdminClient); |
| 83 | + } |
| 84 | + } |
| 85 | + nextPageToken = response.getOpcNextPage(); |
| 86 | + } while (nextPageToken != null); |
| 87 | + } catch (Exception e) { |
| 88 | + e.printStackTrace(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static void listStreamsInCompartment(String id, StreamAdminClient streamAdminClient) { |
| 93 | + ListStreamsRequest request = ListStreamsRequest.builder().compartmentId(id).build(); |
| 94 | + |
| 95 | + ListStreamsResponse listResponse = streamAdminClient.listStreams(request); |
| 96 | + String streams = listResponse.getItems().stream().map(resp -> resp.toString()).collect(Collectors.joining(",")); |
| 97 | + System.out.println("Resp = " + streams); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments