Skip to content

Instantly share code, notes, and snippets.

@tombuildsstuff
Last active November 14, 2023 08:39
Show Gist options
  • Save tombuildsstuff/70a174c4dc8a3e6111e659a9d28df0ea to your computer and use it in GitHub Desktop.
Save tombuildsstuff/70a174c4dc8a3e6111e659a9d28df0ea to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-02-01-preview/serverazureadonlyauthentications"
"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
"github.com/hashicorp/go-azure-sdk/sdk/environments"
)
func main() {
if err := run(context.TODO()); err != nil {
log.Fatalf(err.Error())
}
}
func run(ctx context.Context) error {
env := environments.AzurePublic()
id := commonids.NewSqlServerID("subscription", "resourceGroup", "name")
// assuming the client is configured with an authorizer etc.
client, err := serverazureadonlyauthentications.NewServerAzureADOnlyAuthenticationsClientWithBaseURI(env.ResourceManager)
if err != nil {
log.Fatalf(err.Error())
}
if _, err := client.Delete(ctx, id); err != nil {
log.Fatalf(err.Error())
}
initialDelayDuration := 10 * time.Second
pollerType := NewSqlServerDeleteServerAzureADOnlyAuthenticationPoller(client, id)
poller := pollers.NewPoller(pollerType, initialDelayDuration, pollers.DefaultNumberOfDroppedConnectionsToAllow)
if err := poller.PollUntilDone(ctx); err != nil {
return err
}
return nil
}
// Within the Provider we're putting Custom Pollers into a `custompollers` folder within the Service Package
// e.g. `/internal/services/sql/custompollers`
var _ pollers.PollerType = &SqlServerDeleteServerAzureADOnlyAuthenticationPoller{}
type SqlServerDeleteServerAzureADOnlyAuthenticationPoller struct {
client *serverazureadonlyauthentications.ServerAzureADOnlyAuthenticationsClient
serverId commonids.SqlServerId
}
func NewSqlServerDeleteServerAzureADOnlyAuthenticationPoller(client *serverazureadonlyauthentications.ServerAzureADOnlyAuthenticationsClient, serverId commonids.SqlServerId) *SqlServerDeleteServerAzureADOnlyAuthenticationPoller {
return &SqlServerDeleteServerAzureADOnlyAuthenticationPoller{
client: client,
serverId: serverId,
}
}
func (p *SqlServerDeleteServerAzureADOnlyAuthenticationPoller) Poll(ctx context.Context) (*pollers.PollResult, error) {
resp, err := p.client.Get(ctx, p.serverId)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", p.serverId, err)
}
if resp.Model == nil {
return nil, fmt.Errorf("retrieving %s: `model` was nil", p.serverId)
}
name := pointer.From(resp.Model.Name)
azureAdOnlyAuthentication := true
if props := resp.Model.Properties; props != nil {
azureAdOnlyAuthentication = props.AzureADOnlyAuthentication
}
if !azureAdOnlyAuthentication && strings.EqualFold(name, "Default") {
return &pollers.PollResult{
Status: pollers.PollingStatusSucceeded,
PollInterval: 10 * time.Second,
}, nil
}
// Processing
return &pollers.PollResult{
Status: pollers.PollingStatusInProgress,
PollInterval: 10 * time.Second,
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment