/
groups.go
55 lines (47 loc) · 1.88 KB
/
groups.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package client
import (
"context"
"fmt"
"time"
azresources "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/Azure/go-autorest/autorest/to"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/openshift/openshift-azure/pkg/util/azureclient"
"github.com/openshift/openshift-azure/pkg/util/azureclient/resources"
)
// EnsureResourceGroup creates a resource group and returns whether the
// resource group was actually created or not and any error encountered.
func EnsureResourceGroup(log *logrus.Entry, conf *Config) error {
authorizer, err := azureclient.NewAuthorizer(conf.ClientID, conf.ClientSecret, conf.TenantID, "")
if err != nil {
return err
}
ctx := context.Background()
gc := resources.NewGroupsClient(ctx, log, conf.SubscriptionID, authorizer)
if ready, _ := checkResourceGroupIsReady(ctx, gc, conf.ResourceGroup); ready {
return nil
}
tags := map[string]*string{
"now": to.StringPtr(fmt.Sprintf("%d", time.Now().Unix())),
"ttl": to.StringPtr("72h"),
}
if conf.ResourceGroupTTL != "" {
if _, err := time.ParseDuration(conf.ResourceGroupTTL); err != nil {
return fmt.Errorf("invalid ttl provided: %q - %v", conf.ResourceGroupTTL, err)
}
tags["ttl"] = &conf.ResourceGroupTTL
}
if _, err = gc.CreateOrUpdate(ctx, conf.ResourceGroup, azresources.Group{Location: &conf.Region, Tags: tags}); err != nil {
return err
}
log.Infof("waiting for successful provision of resource group %s", conf.ResourceGroup)
return wait.PollImmediate(5*time.Second, 5*time.Minute, func() (bool, error) { return checkResourceGroupIsReady(ctx, gc, conf.ResourceGroup) })
}
func checkResourceGroupIsReady(ctx context.Context, gc resources.GroupsClient, resourceGroup string) (bool, error) {
g, err := gc.Get(ctx, resourceGroup)
if err != nil {
return false, err
}
return *g.Properties.ProvisioningState == "Succeeded", nil
}