https://pulumi.com logo
#golang
Title
# golang
b

bumpy-byte-21437

07/21/2022, 12:19 PM
Copy code
package aks

import (
  "fmt"

  "<http://github.com/pulumi/pulumi-azure-native/sdk/go/azure/containerservice|github.com/pulumi/pulumi-azure-native/sdk/go/azure/containerservice>"
  "<http://github.com/pulumi/pulumi-azure/sdk/v5/go/azure/core|github.com/pulumi/pulumi-azure/sdk/v5/go/azure/core>"
  "<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi|github.com/pulumi/pulumi/sdk/v3/go/pulumi>"
)

type Aks struct {
  pulumi.ResourceState
  resourceId pulumi.StringOutput
  name       pulumi.StringOutput
}

type AksArgs struct {
  EnableRBAC        pulumi.BoolInput
  ResourceName      pulumi.StringInput
  KubernetesVersion pulumi.StringInput
  NodeResourceGroup pulumi.StringInput
  ResourceGroup     pulumi.StringInput
  NodeCount         pulumi.IntInput
  Location          pulumi.StringInput
}

func CreateAks(ctx *pulumi.Context, name string, args *AksArgs, opts ...pulumi.ResourceOption) (*Aks, error) {
  aks := &Aks{}

  err := ctx.RegisterComponentResource("resource:index:Aks", name, aks, opts...)
  if err != nil {
    return nil, err
  }

  rgs, err := core.NewResourceGroup(ctx, "resourceGroups", &core.ResourceGroupArgs{
    Name:     args.ResourceGroup,
    Location: args.Location,
  }, pulumi.Parent(aks))

  if err != nil {
    return nil, fmt.Errorf("error creating resource group: %v", err)
  }

  k8s, err := containerservice.NewManagedCluster(ctx, "akss", &containerservice.ManagedClusterArgs{
    Location: rgs.Location,
    AgentPoolProfiles: containerservice.ManagedClusterAgentPoolProfileArray{
      &containerservice.ManagedClusterAgentPoolProfileArgs{
        Count:              args.NodeCount,
        EnableAutoScaling:  pulumi.Bool(false),
        EnableNodePublicIP: pulumi.Bool(true),
        Mode:               pulumi.String("System"),
        Name:               pulumi.String("default"),
        OsType:             pulumi.String("Linux"),
        Type:               pulumi.String("VirtualMachineScaleSets"),
        VmSize:             pulumi.String("Standard_B2s"),
      },
    },
    DnsPrefix:         pulumi.String("akss"),
    EnableRBAC:        args.EnableRBAC,
    Identity:          &containerservice.ManagedClusterIdentityArgs{Type: containerservice.ResourceIdentityTypeSystemAssigned},
    KubernetesVersion: args.KubernetesVersion,
    NodeResourceGroup: args.NodeResourceGroup,
    ResourceGroupName: rgs.Name,
    ResourceName:      args.ResourceName,
    Sku: &containerservice.ManagedClusterSKUArgs{
      Name: pulumi.String("Basic"),
      Tier: pulumi.String("Free"),
    },
  }, pulumi.Parent(rgs))

  if err != nil {
    return nil, fmt.Errorf("error creating cluster: %v", err)
  }

  ctx.RegisterResourceOutputs(aks, pulumi.Map{
    "resourceId": k8s.ID(),
    "name":       k8s.Name,
  })
  return aks, nil
}
q

quiet-laptop-13439

07/21/2022, 1:07 PM
you need to
ctx.Export()
them from your pulumi main function that uses your
NewAks
b

bumpy-byte-21437

07/21/2022, 1:11 PM
you have some sample somewhere?
@quiet-laptop-13439 if you want to use this output as input for another function you also should do this in the main function right?
q

quiet-laptop-13439

07/21/2022, 2:27 PM
no, you create a field in your Aks struct and access that field directly
e.g. aks, err := NewAks(...)
somethingElseArgs:= { AksId: aks.ID() }
if you want k8s.Name there, just create Name in Aks struct, and at the end of your NewAks assign
aks.Name = k8s.Name.ToStringOutput()
, or something similar
b

bumpy-byte-21437

07/21/2022, 2:31 PM
Also when i have two different pacackes? aks.go and acr.go are in different packages
In the main.go file i reference those functions
So if i want to use output between functions i can do this directly?
q

quiet-laptop-13439

07/21/2022, 2:34 PM
cant make too many assumptions about your package structure, but if you can pass outputs via args after assignment, similar to terraform
b

bumpy-byte-21437

07/21/2022, 2:35 PM
Yeah terraform is referencing outputs through main
b

billowy-army-68599

07/21/2022, 2:52 PM
b

bumpy-byte-21437

07/21/2022, 3:02 PM
@billowy-army-68599 the complete code is at https://github.com/dkooll/pulumi-go-webapp
Will have a look at your examples
my main question would be how to use for example something like a aks cluster ID as input in the ACR package with the current package structure using different functions and packages
resource components should be ok in this sample right?
@billowy-army-68599 you have some time to look? 😀
b

billowy-army-68599

07/21/2022, 5:21 PM
If I do it won't be for a couple days I'm afraid
b

bumpy-byte-21437

07/22/2022, 11:25 AM
@quiet-laptop-13439, @billowy-army-68599 i got a working output now using
Copy code
aksnames := k8s.Name.ApplyT(func(aksname string) string {
    return aksname
  }).(pulumi.StringOutput)

  aks.AksNames = aksnames

  if err := ctx.RegisterResourceOutputs(aks, pulumi.Map{
    "aksnames": aksnames,
  }); err != nil {
    return nil, err
  }

  ctx.Export("AksNames", aks.AksNames)
  return aks, nil
}
you have any idea how i get the id instead of the name
q

quiet-laptop-13439

07/22/2022, 11:27 AM
you dont need that applyt, it does nothing
b

bumpy-byte-21437

07/22/2022, 11:27 AM
ahk.. thanks.. according to the docs Id should be available as output
q

quiet-laptop-13439

07/22/2022, 11:28 AM
for id just do the same assignment
as for the name, just type is different
b

bumpy-byte-21437

07/22/2022, 11:29 AM
it give me a
k8s.Id undefined (type *containerservice.ManagedCluster has no field or method Id)
using
Copy code
aksnames := k8s.Id.ApplyT(func(aksname string) string {
    return aksname
  }).(pulumi.StringOutput)
am i missing something
q

quiet-laptop-13439

07/22/2022, 11:35 AM
im not following why did you do that
first, as i said, you dont need that applyt
id is k8s.ID()
b

bumpy-byte-21437

07/22/2022, 11:39 AM
can you provide a more complete sample to make this a output using component resources, cause this aint helping much
q

quiet-laptop-13439

07/22/2022, 11:54 AM
I'm on a phone
do you intend aksnames to be an array?
Copy code
aks.AksNames = k8s.Name
then
Copy code
aks.ResourceId := k8s.ID() // make sure Aks struct field is ResourceId
remove ctx.Export from the module, do it in main
Copy code
ctx.Export("k8s_resource_id", aks.ResourceId)
b

bumpy-byte-21437

07/22/2022, 12:03 PM
hmm thanks.. only getting a
expected identifier on left side of :=
on this one aks.ResourceId := k8s.ID()
for example
aks.ResourceState := k8s.State()
works, but
aks.ResourceId := k8s.ID()
gives me a
expected identifier on left side of :=
@quiet-laptop-13439 if i remove the semicolon i get a
cannot use k8s.ID() (value of type pulumi.IDOutput) as pulumi.StringOutput value in assignment
so this one works
aks.ResourceId = k8s.ID().ToStringOutput()
so this is working.. now how to pass this output which is in the aks package as input to a resource in the acr package?
do i have to pass this in my main function?
8 Views