Created
July 25, 2019 09:03
-
-
Save jkrajniak/a3717b6afa441cd3c6f41d84d614c779 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"strings" | |
"time" | |
"github.com/pkg/errors" | |
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2016-06-01/recoveryservices" | |
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2017-07-01/backup" | |
"github.com/Azure/go-autorest/autorest" | |
"github.com/Azure/go-autorest/autorest/azure/auth" | |
) | |
type AzureCredentials struct { | |
ClientID string `json:"client_id"` | |
ClientSecret string `json:"client_secret"` | |
SubscriptionID string `json:"subscription_id"` | |
TenantID string `json:"tenant_id"` | |
} | |
func (ac AzureCredentials) GetAuthorizerFromCredentials() (autorest.Authorizer, error) { | |
c := auth.NewClientCredentialsConfig(ac.ClientID, ac.ClientSecret, ac.TenantID) | |
return c.Authorizer() | |
} | |
type AzureSession struct { | |
SubscriptionID string | |
Authorizer autorest.Authorizer | |
Created time.Time | |
} | |
func NewSession(authCredentials AzureCredentials) (*AzureSession, error) { | |
authorizer, err := authCredentials.GetAuthorizerFromCredentials() | |
if err != nil { | |
return nil, errors.Wrap(err, "cannot get initial session") | |
} | |
sess := AzureSession{ | |
SubscriptionID: authCredentials.SubscriptionID, | |
Authorizer: authorizer, | |
Created: time.Now(), | |
} | |
return &sess, err | |
} | |
func main() { | |
credentials := AzureCredentials{ | |
ClientID: "...", | |
ClientSecret: "...", | |
SubscriptionID: "...", | |
TenantID: "...", | |
} | |
session, _ := NewSession(credentials) | |
vaultClient := recoveryservices.NewVaultsClient(credentials.SubscriptionID) | |
vaultClient.Authorizer = session.Authorizer | |
jobsClient := backup.NewJobsClient(credentials.SubscriptionID) | |
jobsClient.Authorizer = session.Authorizer | |
for vaultPage, err := vaultClient.ListBySubscriptionIDComplete(context.Background()); vaultPage.NotDone(); err = vaultPage.NextWithContext(context.Background()) { | |
if err != nil { | |
panic(err) | |
} | |
resVal := vaultPage.Value() | |
resourceGroupName := strings.Split(*resVal.ID, "/")[4] | |
for jobPage, err := jobsClient.ListComplete(context.Background(), *resVal.Name, resourceGroupName, "", ""); jobPage.NotDone(); err = jobPage.NextWithContext(context.Background()) { | |
if err != nil { | |
panic(errors.Wrap(err, "list jobs")) | |
} | |
jobVal := jobPage.Value() | |
fmt.Println(jobVal) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment