1// Copyright 2019 Google LLC 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// https://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// Code generated by gapic-generator. DO NOT EDIT. 16 17package containeranalysis 18 19import ( 20 "context" 21 "fmt" 22 "net/url" 23 24 grafeas "cloud.google.com/go/grafeas/apiv1" 25 gax "github.com/googleapis/gax-go/v2" 26 "google.golang.org/api/option" 27 gtransport "google.golang.org/api/transport/grpc" 28 containeranalysispb "google.golang.org/genproto/googleapis/devtools/containeranalysis/v1" 29 iampb "google.golang.org/genproto/googleapis/iam/v1" 30 "google.golang.org/grpc" 31 "google.golang.org/grpc/metadata" 32) 33 34// CallOptions contains the retry settings for each method of Client. 35type CallOptions struct { 36 SetIamPolicy []gax.CallOption 37 GetIamPolicy []gax.CallOption 38 TestIamPermissions []gax.CallOption 39} 40 41func defaultClientOptions() []option.ClientOption { 42 return []option.ClientOption{ 43 option.WithEndpoint("containeranalysis.googleapis.com:443"), 44 option.WithScopes(DefaultAuthScopes()...), 45 } 46} 47 48func defaultCallOptions() *CallOptions { 49 retry := map[[2]string][]gax.CallOption{} 50 return &CallOptions{ 51 SetIamPolicy: retry[[2]string{"default", "non_idempotent"}], 52 GetIamPolicy: retry[[2]string{"default", "non_idempotent"}], 53 TestIamPermissions: retry[[2]string{"default", "non_idempotent"}], 54 } 55} 56 57// Client is a client for interacting with Container Analysis API. 58// 59// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 60type Client struct { 61 // The connection to the service. 62 connPool gtransport.ConnPool 63 64 // The gRPC API client. 65 client containeranalysispb.ContainerAnalysisClient 66 67 // A pre-crearted grafeas client. 68 grafeasClient *grafeas.Client 69 70 // The call options for this service. 71 CallOptions *CallOptions 72 73 // The x-goog-* metadata to be sent with each request. 74 xGoogMetadata metadata.MD 75} 76 77// NewClient creates a new container analysis client. 78// 79// Retrieves analysis results of Cloud components such as Docker container 80// images. The Container Analysis API is an implementation of the 81// Grafeas (at https://grafeas.io) API. 82// 83// Analysis results are stored as a series of occurrences. An Occurrence 84// contains information about a specific analysis instance on a resource. An 85// occurrence refers to a Note. A note contains details describing the 86// analysis and is generally stored in a separate project, called a Provider. 87// Multiple occurrences can refer to the same note. 88// 89// For example, an SSL vulnerability could affect multiple images. In this case, 90// there would be one note for the vulnerability and an occurrence for each 91// image with the vulnerability referring to that note. 92func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) { 93 connPool, err := gtransport.DialPool(ctx, append(defaultClientOptions(), opts...)...) 94 if err != nil { 95 return nil, err 96 } 97 gc, err := grafeas.NewClient(ctx, append(defaultClientOptions(), opts...)...) 98 if err != nil { 99 return nil, err 100 } 101 c := &Client{ 102 connPool: connPool, 103 CallOptions: defaultCallOptions(), 104 105 client: containeranalysispb.NewContainerAnalysisClient(connPool), 106 grafeasClient: gc, 107 } 108 c.setGoogleClientInfo() 109 return c, nil 110} 111 112// GetGrafeasClient returns a grafeas client connected to containeranalysis. 113// 114// Calling Close on either the grafeas or containeranalysis client will close 115// the shared connection in both. 116func (c *Client) GetGrafeasClient() *grafeas.Client { 117 return c.grafeasClient 118} 119 120// Connection returns the client's connection to the API service. 121func (c *Client) Connection() *grpc.ClientConn { 122 return c.connPool.Conn() 123} 124 125// Close closes the connection to the API service. The user should invoke this when 126// the client is no longer required. 127func (c *Client) Close() error { 128 return c.connPool.Close() 129} 130 131// setGoogleClientInfo sets the name and version of the application in 132// the `x-goog-api-client` header passed on each request. Intended for 133// use by Google-written clients. 134func (c *Client) setGoogleClientInfo(keyval ...string) { 135 kv := append([]string{"gl-go", versionGo()}, keyval...) 136 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 137 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 138} 139 140// SetIamPolicy sets the access control policy on the specified note or occurrence. 141// Requires containeranalysis.notes.setIamPolicy or 142// containeranalysis.occurrences.setIamPolicy permission if the resource is 143// a note or an occurrence, respectively. 144// 145// The resource takes the format projects/[PROJECT_ID]/notes/[NOTE_ID] for 146// notes and projects/[PROJECT_ID]/occurrences/[OCCURRENCE_ID] for 147// occurrences. 148func (c *Client) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 149 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))) 150 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 151 opts = append(c.CallOptions.SetIamPolicy[0:len(c.CallOptions.SetIamPolicy):len(c.CallOptions.SetIamPolicy)], opts...) 152 var resp *iampb.Policy 153 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 154 var err error 155 resp, err = c.client.SetIamPolicy(ctx, req, settings.GRPC...) 156 return err 157 }, opts...) 158 if err != nil { 159 return nil, err 160 } 161 return resp, nil 162} 163 164// GetIamPolicy gets the access control policy for a note or an occurrence resource. 165// Requires containeranalysis.notes.setIamPolicy or 166// containeranalysis.occurrences.setIamPolicy permission if the resource is 167// a note or occurrence, respectively. 168// 169// The resource takes the format projects/[PROJECT_ID]/notes/[NOTE_ID] for 170// notes and projects/[PROJECT_ID]/occurrences/[OCCURRENCE_ID] for 171// occurrences. 172func (c *Client) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 173 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))) 174 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 175 opts = append(c.CallOptions.GetIamPolicy[0:len(c.CallOptions.GetIamPolicy):len(c.CallOptions.GetIamPolicy)], opts...) 176 var resp *iampb.Policy 177 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 178 var err error 179 resp, err = c.client.GetIamPolicy(ctx, req, settings.GRPC...) 180 return err 181 }, opts...) 182 if err != nil { 183 return nil, err 184 } 185 return resp, nil 186} 187 188// TestIamPermissions returns the permissions that a caller has on the specified note or 189// occurrence. Requires list permission on the project (for example, 190// containeranalysis.notes.list). 191// 192// The resource takes the format projects/[PROJECT_ID]/notes/[NOTE_ID] for 193// notes and projects/[PROJECT_ID]/occurrences/[OCCURRENCE_ID] for 194// occurrences. 195func (c *Client) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) { 196 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))) 197 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 198 opts = append(c.CallOptions.TestIamPermissions[0:len(c.CallOptions.TestIamPermissions):len(c.CallOptions.TestIamPermissions)], opts...) 199 var resp *iampb.TestIamPermissionsResponse 200 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 201 var err error 202 resp, err = c.client.TestIamPermissions(ctx, req, settings.GRPC...) 203 return err 204 }, opts...) 205 if err != nil { 206 return nil, err 207 } 208 return resp, nil 209} 210