|
| 1 | +// Copyright 2020 StrongDM Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +// Code generated by protogen. DO NOT EDIT. |
| 17 | + |
| 18 | +package com.strongdm.api; |
| 19 | + |
| 20 | +import com.strongdm.api.plumbing.PageIterator; |
| 21 | +import com.strongdm.api.plumbing.PageResult; |
| 22 | +import com.strongdm.api.plumbing.Plumbing; |
| 23 | +import com.strongdm.api.plumbing.RequestableAccountEntitlementsGrpc; |
| 24 | +import com.strongdm.api.plumbing.RequestableAccountEntitlementsPlumbing; |
| 25 | +import com.strongdm.api.plumbing.Spec.ListRequestMetadata; |
| 26 | +import io.grpc.Deadline; |
| 27 | +import io.grpc.ManagedChannel; |
| 28 | +import java.util.Iterator; |
| 29 | +import java.util.List; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import java.util.function.Supplier; |
| 32 | + |
| 33 | +/** |
| 34 | + * RequestableAccountEntitlements enumerates the resources that an account is permitted to request |
| 35 | + * access to. The RequestableAccountEntitlements service is read-only. |
| 36 | + */ |
| 37 | +public class RequestableAccountEntitlements implements SnapshotRequestableAccountEntitlements { |
| 38 | + private final RequestableAccountEntitlementsGrpc.RequestableAccountEntitlementsBlockingStub stub; |
| 39 | + private final Client parent; |
| 40 | + private final Deadline deadline; |
| 41 | + |
| 42 | + public RequestableAccountEntitlements(ManagedChannel channel, Client client) { |
| 43 | + this.stub = RequestableAccountEntitlementsGrpc.newBlockingStub(channel); |
| 44 | + this.parent = client; |
| 45 | + this.deadline = null; |
| 46 | + } |
| 47 | + |
| 48 | + private RequestableAccountEntitlements( |
| 49 | + RequestableAccountEntitlementsGrpc.RequestableAccountEntitlementsBlockingStub stub, |
| 50 | + Client client, |
| 51 | + Deadline deadline) { |
| 52 | + this.stub = stub; |
| 53 | + this.parent = client; |
| 54 | + this.deadline = deadline; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * This function returns a copy of the RequestableAccountEntitlements service which has the given |
| 59 | + * deadline set for all method calls. |
| 60 | + */ |
| 61 | + public RequestableAccountEntitlements withDeadlineAfter(long duration, TimeUnit units) { |
| 62 | + Deadline deadline = Deadline.after(duration, units); |
| 63 | + return new RequestableAccountEntitlements( |
| 64 | + this.stub.withDeadline(deadline), this.parent, deadline); |
| 65 | + } |
| 66 | + /** List gets a list of RequestableAccountEntitlement records matching a given set of criteria. */ |
| 67 | + public Iterable<RequestableAccountEntitlement> list( |
| 68 | + String accountId, String filter, Object... args) throws RpcException { |
| 69 | + RequestableAccountEntitlementsPlumbing.RequestableAccountEntitlementListRequest.Builder |
| 70 | + builder = |
| 71 | + RequestableAccountEntitlementsPlumbing.RequestableAccountEntitlementListRequest |
| 72 | + .newBuilder(); |
| 73 | + builder.setAccountId((accountId)); |
| 74 | + builder.setFilter(Plumbing.quoteFilterArgs(filter, args)); |
| 75 | + ListRequestMetadata.Builder metaBuilder = ListRequestMetadata.newBuilder(); |
| 76 | + if (this.parent.pageLimit > 0) { |
| 77 | + metaBuilder.setLimit(this.parent.pageLimit); |
| 78 | + } |
| 79 | + if (this.parent.snapshotDate != null) { |
| 80 | + metaBuilder.setSnapshotAt(Plumbing.convertTimestampToPlumbing(this.parent.snapshotDate)); |
| 81 | + } |
| 82 | + builder.setMeta(metaBuilder); |
| 83 | + |
| 84 | + Supplier<PageResult<RequestableAccountEntitlement>> pageFetcher = |
| 85 | + () -> { |
| 86 | + // Note: this closure captures and reuses the builder to set the next page |
| 87 | + RequestableAccountEntitlementsPlumbing.RequestableAccountEntitlementListRequest req = |
| 88 | + builder.build(); |
| 89 | + RequestableAccountEntitlementsPlumbing.RequestableAccountEntitlementListResponse |
| 90 | + plumbingResponse; |
| 91 | + int tries = 0; |
| 92 | + while (true) { |
| 93 | + try { |
| 94 | + plumbingResponse = |
| 95 | + this.stub |
| 96 | + .withCallCredentials( |
| 97 | + this.parent.getCallCredentials( |
| 98 | + "RequestableAccountEntitlements.List", req)) |
| 99 | + .list(req); |
| 100 | + } catch (Exception e) { |
| 101 | + if (this.parent.shouldRetry(tries, e, this.deadline)) { |
| 102 | + tries++; |
| 103 | + try { |
| 104 | + Thread.sleep(this.parent.exponentialBackoff(tries, this.deadline)); |
| 105 | + } catch (Exception ignored) { |
| 106 | + } |
| 107 | + continue; |
| 108 | + } |
| 109 | + throw Plumbing.convertExceptionToPorcelain(e); |
| 110 | + } |
| 111 | + break; |
| 112 | + } |
| 113 | + |
| 114 | + List<RequestableAccountEntitlement> page = |
| 115 | + Plumbing.convertRepeatedRequestableAccountEntitlementToPorcelain( |
| 116 | + plumbingResponse.getRequestableAccountEntitlementsList()); |
| 117 | + |
| 118 | + boolean hasNextCursor = plumbingResponse.getMeta().getNextCursor() != ""; |
| 119 | + builder.setMeta( |
| 120 | + ListRequestMetadata.newBuilder() |
| 121 | + .setCursor(plumbingResponse.getMeta().getNextCursor())); |
| 122 | + |
| 123 | + return new PageResult<RequestableAccountEntitlement>(page, hasNextCursor); |
| 124 | + }; |
| 125 | + |
| 126 | + Iterator<RequestableAccountEntitlement> iterator = new PageIterator<>(pageFetcher); |
| 127 | + |
| 128 | + return () -> iterator; |
| 129 | + } |
| 130 | +} |
0 commit comments