Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial contribution of Azure module #332

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions modules/azure/acr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package azure

import (
"context"
"testing"

"github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2017-10-01/containerregistry"
)

func registriesClientE(subscriptionID string) (*containerregistry.RegistriesClient, error) {
authorizer, err := DeploymentServicePrincipalAuthorizer()
if err != nil {
return nil, err
}
client := containerregistry.NewRegistriesClient(subscriptionID)
client.Authorizer = authorizer
return &client, nil
}

func webhookClientE(subscriptionID string) (*containerregistry.WebhooksClient, error) {
authorizer, err := DeploymentServicePrincipalAuthorizer()
if err != nil {
return nil, err
}
client := containerregistry.NewWebhooksClient(subscriptionID)
client.Authorizer = authorizer
return &client, nil
}

// ACRNetworkAclsE - Return the newtwork ACLs for an ACR instance
func ACRNetworkAclsE(subscriptionID string, resourceGroupName string, acrName string) (*containerregistry.NetworkRuleSet, error) {

client, err := registriesClientE(subscriptionID)
if err != nil {
return nil, err
}

acr, err := client.Get(context.Background(), resourceGroupName, acrName)
if err != nil {
return nil, err
}

return acr.NetworkRuleSet, nil
}

// ACRNetworkAcls - Like ACRNetworkAclsE but fails in the case an error is returned
func ACRNetworkAcls(t *testing.T, subscriptionID string, resourceGroupName string, acrName string) *containerregistry.NetworkRuleSet {
acls, err := ACRNetworkAclsE(subscriptionID, resourceGroupName, acrName)
if err != nil {
t.Fatal(err)
}
return acls
}

// ACRWebHookE - Return ACR Webhook definition
func ACRWebHookE(subscriptionID string, resourceGroupName string, acrName string, webhookName string) (*containerregistry.Webhook, error) {
client, err := webhookClientE(subscriptionID)
if err != nil {
return nil, err
}

webhook, err := client.Get(context.Background(), resourceGroupName, acrName, webhookName)
if err != nil {
return nil, err
}

return &webhook, nil
}

// ACRWebHook - Like ACRWebHookE but fails in the case an error is returned
func ACRWebHook(t *testing.T, subscriptionID string, resourceGroupName string, acrName string, webhookName string) *containerregistry.Webhook {
webhooks, err := ACRWebHookE(subscriptionID, resourceGroupName, acrName, webhookName)
if err != nil {
t.Fatal(err)
}
return webhooks
}

// ACRWebHookCallbackE - Get callback config for a webhook
func ACRWebHookCallbackE(subscriptionID string, resourceGroupName string, acrName string, webhookName string) (*containerregistry.CallbackConfig, error) {
client, err := webhookClientE(subscriptionID)
if err != nil {
return nil, err
}

webhookCallback, err := client.GetCallbackConfig(context.Background(), resourceGroupName, acrName, webhookName)
if err != nil {
return nil, err
}

return &webhookCallback, nil
}

// ACRWebHookCallback - Like ACRWebHookCallbackE but fails in the case an error is returned
func ACRWebHookCallback(t *testing.T, subscriptionID string, resourceGroupName string, acrName string, webhookName string) *containerregistry.CallbackConfig {
webhookCallback, err := ACRWebHookCallbackE(subscriptionID, resourceGroupName, acrName, webhookName)
if err != nil {
t.Fatal(err)
}
return webhookCallback
}
49 changes: 49 additions & 0 deletions modules/azure/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package azure

import (
"context"
"testing"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
)

func vnetClient(subscriptionID string) (*network.VirtualNetworksClient, error) {
authorizer, err := DeploymentServicePrincipalAuthorizer()
if err != nil {
return nil, err
}

client := network.NewVirtualNetworksClient(subscriptionID)
client.Authorizer = authorizer
return &client, err
}

// VnetSubnetsListE - Return the subnets that exist wihin a given VNET
func VnetSubnetsListE(subscriptionID string, resourceGroupName string, vnetName string) ([]string, error) {

client, err := vnetClient(subscriptionID)
if err != nil {
return nil, err
}

vnet, err := client.Get(context.Background(), resourceGroupName, vnetName, "")
if err != nil {
return nil, err
}

subnets := make([]string, len(*vnet.VirtualNetworkPropertiesFormat.Subnets))
for index, subnet := range *vnet.VirtualNetworkPropertiesFormat.Subnets {
subnets[index] = *subnet.ID
}

return subnets, nil
}

// VnetSubnetsList - Like VnetSubnetsListE but fails in the case an error is returned
func VnetSubnetsList(t *testing.T, subscriptionID string, resourceGroupName string, vnetName string) []string {
subnets, err := VnetSubnetsListE(subscriptionID, resourceGroupName, vnetName)
if err != nil {
t.Fatal(err)
}
return subnets
}