Skip to content

Commit

Permalink
feat(securitycenter): add sample for creating v2 clients with regiona…
Browse files Browse the repository at this point in the history
…l and default endpoints
  • Loading branch information
hegemonic committed Oct 2, 2024
1 parent 7c4f707 commit 797bb67
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
33 changes: 33 additions & 0 deletions securitycenter/clientv2/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Google LLC
//
// 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
//
// https://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 clientv2

import (
"os"
"testing"
)

func TestMain(m *testing.M) {
code := m.Run()
os.Exit(code)
}

func TestCreateClientWithEndpoint(t *testing.T) {
loc := "me-central2"
err := createClientWithEndpoint(loc)
if err != nil {
t.Errorf("createClientWithEndpoint(%s) had error: %v", loc, err)
}
}
50 changes: 50 additions & 0 deletions securitycenter/clientv2/create_client_with_endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024 Google LLC
//
// 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
//
// https://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 clientv2

// [START securitycenter_set_client_endpoint_v2]
import (
"context"
"fmt"

securitycenter "cloud.google.com/go/securitycenter/apiv2"
"google.golang.org/api/option"
)

// createClientWithEndpoint creates a Security Command Center client for a
// regional endpoint, along with another client for the default endpoint.
func createClientWithEndpoint(repLocation string) error {
// Instantiate client for default endpoint.
ctx := context.Background()
client, err := securitycenter.NewClient(ctx)
if err != nil {
return fmt.Errorf("securitycenter.NewClient: %w", err)
}
defer client.Close() // Closing the client safely cleans up background resources.

// Assemble the regional endpoint URL using provided location.
repEndpoint := fmt.Sprintf("securitycenter.%s.rep.googleapis.com:443", repLocation)
// Instantiate client for regional endpoint.
repCtx := context.Background()
repClient, err := securitycenter.NewClient(repCtx, option.WithEndpoint(repEndpoint))
if err != nil {
return fmt.Errorf("securitycenter.NewClient: %w", err)
}
defer repClient.Close() // Closing the client safely cleans up background resources.

return nil
}

// [END securitycenter_set_client_endpoint_v2]

0 comments on commit 797bb67

Please sign in to comment.