Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-zhenhua committed Feb 14, 2022
1 parent d8ceb8f commit e312a88
Show file tree
Hide file tree
Showing 29 changed files with 2,031 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
iothub "github.com/hashicorp/terraform-provider-azurerm/internal/services/iothub/client"
timeseriesinsights "github.com/hashicorp/terraform-provider-azurerm/internal/services/iottimeseriesinsights/client"
keyvault "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/client"
kubernetes "github.com/hashicorp/terraform-provider-azurerm/internal/services/kubernetes/client"
kusto "github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto/client"
lighthouse "github.com/hashicorp/terraform-provider-azurerm/internal/services/lighthouse/client"
loadbalancers "github.com/hashicorp/terraform-provider-azurerm/internal/services/loadbalancer/client"
Expand Down Expand Up @@ -166,6 +167,7 @@ type Client struct {
IoTHub *iothub.Client
IoTTimeSeriesInsights *timeseriesinsights.Client
KeyVault *keyvault.Client
Kubernetes *kubernetes.Client
Kusto *kusto.Client
Lighthouse *lighthouse.Client
LoadBalancers *loadbalancers.Client
Expand Down Expand Up @@ -278,6 +280,7 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
client.IoTHub = iothub.NewClient(o)
client.IoTTimeSeriesInsights = timeseriesinsights.NewClient(o)
client.KeyVault = keyvault.NewClient(o)
client.Kubernetes = kubernetes.NewClient(o)
client.Kusto = kusto.NewClient(o)
client.Lighthouse = lighthouse.NewClient(o)
client.LogAnalytics = loganalytics.NewClient(o)
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/iothub"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/iottimeseriesinsights"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/kubernetes"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/lighthouse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/loadbalancer"
Expand Down Expand Up @@ -181,6 +182,7 @@ func SupportedUntypedServices() []sdk.UntypedServiceRegistration {
iothub.Registration{},
iotcentral.Registration{},
keyvault.Registration{},
kubernetes.Registration{},
kusto.Registration{},
loadbalancer.Registration{},
loganalytics.Registration{},
Expand Down
20 changes: 20 additions & 0 deletions internal/services/kubernetes/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package client

import (
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/kubernetes/sdk/2021-10-01/kubernetes"
)

type Client struct {
KubernetesClient *kubernetes.KubernetesClient
}

func NewClient(o *common.ClientOptions) *Client {

kubernetesClient := kubernetes.NewKubernetesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&kubernetesClient.Client, o.ResourceManagerAuthorizer)

return &Client{
KubernetesClient: &kubernetesClient,
}
}
206 changes: 206 additions & 0 deletions internal/services/kubernetes/kubernetes_connected_cluster_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package kubernetes

import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
tagsHelper "github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/kubernetes/sdk/2021-10-01/kubernetes"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceKubernetesConnectedCluster() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceKubernetesConnectedClusterCreate,
Read: resourceKubernetesConnectedClusterRead,
Update: resourceKubernetesConnectedClusterUpdate,
Delete: resourceKubernetesConnectedClusterDelete,

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := kubernetes.ParseConnectedClusterID(id)
return err
}),

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"resource_group_name": azure.SchemaResourceGroupName(),

"agent_public_key_certificate": {
Type: pluginsdk.TypeString,
ForceNew: true,
Required: true,
},

"distribution": {
Type: pluginsdk.TypeString,
ForceNew: true,
Optional: true,
},

"identity": commonschema.SystemAssignedIdentityRequired(),

"infrastructure": {
Type: pluginsdk.TypeString,
ForceNew: true,
Optional: true,
},

"location": azure.SchemaLocation(),

"provisioning_state": {
Type: pluginsdk.TypeString,
Computed: true,
},

"tags": tags.Schema(),
},
}
}

func resourceKubernetesConnectedClusterCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Kubernetes.KubernetesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := kubernetes.NewConnectedClusterID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.ConnectedClusterGet(ctx, id)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}

if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_kubernetes_connected_cluster", id.ID())
}
}

identity, err := identity.ExpandSystemAssigned(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}

location := azure.NormalizeLocation(d.Get("location"))
props := kubernetes.ConnectedCluster{
Identity: *identity,
Location: location,
Properties: kubernetes.ConnectedClusterProperties{
AgentPublicKeyCertificate: d.Get("agent_public_key_certificate").(string),
Distribution: utils.String(d.Get("distribution").(string)),
Infrastructure: utils.String(d.Get("infrastructure").(string)),
},
Tags: tagsHelper.Expand(d.Get("tags").(map[string]interface{})),
}

if err := client.ConnectedClusterCreateThenPoll(ctx, id, props); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

d.SetId(id.ID())
return resourceKubernetesConnectedClusterRead(d, meta)
}

func resourceKubernetesConnectedClusterRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Kubernetes.KubernetesClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := kubernetes.ParseConnectedClusterID(d.Id())
if err != nil {
return err
}

resp, err := client.ConnectedClusterGet(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
}

return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.Set("name", id.ClusterName)
d.Set("resource_group_name", id.ResourceGroupName)
if model := resp.Model; model != nil {
if err := d.Set("identity", identity.FlattenSystemAssigned(&model.Identity)); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

d.Set("location", location.Normalize(model.Location))
props := model.Properties
d.Set("agent_public_key_certificate", props.AgentPublicKeyCertificate)
d.Set("distribution", props.Distribution)
d.Set("infrastructure", props.Infrastructure)
d.Set("provisioning_state", props.ProvisioningState)

if err := tagsHelper.FlattenAndSet(d, model.Tags); err != nil {
return err
}
}

return nil
}

func resourceKubernetesConnectedClusterUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Kubernetes.KubernetesClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := kubernetes.ParseConnectedClusterID(d.Id())
if err != nil {
return err
}

props := kubernetes.ConnectedClusterPatch{
Tags: tagsHelper.Expand(d.Get("tags").(map[string]interface{})),
}

if _, err := client.ConnectedClusterUpdate(ctx, *id, props); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

return resourceKubernetesConnectedClusterRead(d, meta)
}

func resourceKubernetesConnectedClusterDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Kubernetes.KubernetesClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := kubernetes.ParseConnectedClusterID(d.Id())
if err != nil {
return err
}

if err := client.ConnectedClusterDeleteThenPoll(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

return nil
}
Loading

0 comments on commit e312a88

Please sign in to comment.