forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistinstances.go
33 lines (28 loc) · 914 Bytes
/
listinstances.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Sample listinstances lists the Cloud SQL instances for a given project ID.
package listinstances
import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/sqladmin/v1beta4"
)
func ListInstances(projectId string) ([]*sqladmin.DatabaseInstance, error) {
// Create an http.Client that uses Application Default Credentials.
hc, err := google.DefaultClient(oauth2.NoContext, sqladmin.SqlserviceAdminScope)
if err != nil {
return nil, err
}
// Create the Google Cloud SQL service.
service, err := sqladmin.New(hc)
if err != nil {
return nil, err
}
// List instances for the project ID.
instances, err := service.Instances.List(projectId).Do()
if err != nil {
return nil, err
}
return instances.Items, nil
}