-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
client.go
82 lines (69 loc) · 2.96 KB
/
client.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package client
import (
"context"
"fmt"
"github.com/Azure/go-autorest/autorest"
"github.com/hashicorp/go-azure-sdk/resource-manager/batch/2023-05-01/application"
"github.com/hashicorp/go-azure-sdk/resource-manager/batch/2023-05-01/batchaccount"
"github.com/hashicorp/go-azure-sdk/resource-manager/batch/2023-05-01/certificate"
"github.com/hashicorp/go-azure-sdk/resource-manager/batch/2023-05-01/pool"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
batchDataplane "github.com/tombuildsstuff/kermit/sdk/batch/2022-01.15.0/batch"
)
type Client struct {
AccountClient *batchaccount.BatchAccountClient
ApplicationClient *application.ApplicationClient
CertificateClient *certificate.CertificateClient
PoolClient *pool.PoolClient
BatchManagementAuthorizer autorest.Authorizer
}
func NewClient(o *common.ClientOptions) (*Client, error) {
accountClient, err := batchaccount.NewBatchAccountClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Account client: %+v", err)
}
o.Configure(accountClient.Client, o.Authorizers.ResourceManager)
applicationClient, err := application.NewApplicationClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Application client: %+v", err)
}
o.Configure(applicationClient.Client, o.Authorizers.ResourceManager)
certificateClient, err := certificate.NewCertificateClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Certificate client: %+v", err)
}
o.Configure(certificateClient.Client, o.Authorizers.ResourceManager)
poolClient, err := pool.NewPoolClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Pool client: %+v", err)
}
o.Configure(poolClient.Client, o.Authorizers.ResourceManager)
return &Client{
AccountClient: accountClient,
ApplicationClient: applicationClient,
CertificateClient: certificateClient,
PoolClient: poolClient,
BatchManagementAuthorizer: o.BatchManagementAuthorizer,
}, nil
}
func (r *Client) JobClient(ctx context.Context, accountId batchaccount.BatchAccountId) (*batchDataplane.JobClient, error) {
// Retrieve the batch account to find the batch account endpoint
accountClient := r.AccountClient
account, err := accountClient.Get(ctx, accountId)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %v", accountId, err)
}
endpoint := ""
if account.Model != nil && account.Model.Properties != nil {
endpoint = fmt.Sprintf("https://%s", *account.Model.Properties.AccountEndpoint)
}
if endpoint == "" {
return nil, fmt.Errorf("retrieving %s: `properties.AccountEndpoint` was empty", accountId)
}
// Copy the client since we'll manipulate its BatchURL
c := batchDataplane.NewJobClient(endpoint)
c.BaseClient.Client.Authorizer = r.BatchManagementAuthorizer
return &c, nil
}