github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/databricks.go (about)

     1  // Copyright 2021 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package azure
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  
    21  	"github.com/Azure/azure-sdk-for-go/services/databricks/mgmt/2018-04-01/databricks"
    22  )
    23  
    24  type DatabricksGenerator struct {
    25  	AzureService
    26  }
    27  
    28  func (az *DatabricksGenerator) listWorkspaces() ([]databricks.Workspace, error) {
    29  	subscriptionID, resourceGroup, authorizer := az.getClientArgs()
    30  	client := databricks.NewWorkspacesClient(subscriptionID)
    31  	client.Authorizer = authorizer
    32  	var (
    33  		iterator databricks.WorkspaceListResultIterator
    34  		err      error
    35  	)
    36  	ctx := context.Background()
    37  	if resourceGroup != "" {
    38  		iterator, err = client.ListByResourceGroupComplete(ctx, resourceGroup)
    39  	} else {
    40  		iterator, err = client.ListBySubscriptionComplete(ctx)
    41  	}
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	var resources []databricks.Workspace
    46  	for iterator.NotDone() {
    47  		item := iterator.Value()
    48  		resources = append(resources, item)
    49  		if err := iterator.NextWithContext(ctx); err != nil {
    50  			log.Println(err)
    51  			return resources, err
    52  		}
    53  	}
    54  	return resources, nil
    55  }
    56  
    57  func (az *DatabricksGenerator) AppendWorkspace(workspace *databricks.Workspace) {
    58  	az.AppendSimpleResource(*workspace.ID, *workspace.Name, "azurerm_databricks_workspace")
    59  }
    60  
    61  func (az *DatabricksGenerator) InitResources() error {
    62  
    63  	workspaces, err := az.listWorkspaces()
    64  	if err != nil {
    65  		return err
    66  	}
    67  	for _, workspace := range workspaces {
    68  		az.AppendWorkspace(&workspace)
    69  	}
    70  	return nil
    71  }