github.com/yanndegat/hiera@v0.6.8/lookup/testdata/hieratestplugin/hieratestplugin.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  
     7  	"github.com/lyraproj/dgo/vf"
     8  
     9  	"github.com/lyraproj/dgo/dgo"
    10  	"github.com/lyraproj/hierasdk/hiera"
    11  	"github.com/lyraproj/hierasdk/plugin"
    12  	"github.com/lyraproj/hierasdk/register"
    13  )
    14  
    15  func main() {
    16  	register.LookupKey(`test_lookup_key`, lookupOption)
    17  	register.DataHash(`test_data_hash`, sampleHash)
    18  	register.DataHash(`test_refuse_to_die`, refuseToDie)
    19  	register.DataHash(`test_panic`, panicAttack)
    20  	register.DataHash(`test_tf_simulation`, tfSimulation)
    21  	plugin.ServeAndExit()
    22  }
    23  
    24  // lookupOption returns the option for the given key or nil if no such option exist
    25  func lookupOption(c hiera.ProviderContext, key string) dgo.Value {
    26  	return c.Option(key)
    27  }
    28  
    29  func sampleHash(c hiera.ProviderContext) dgo.Map {
    30  	h := c.Option(`the_hash`).(dgo.Map)
    31  	if h.Get(`c`) != nil {
    32  		h = h.Merge(vf.Map(`d`, `interpolate c is %{lookup("c")}`))
    33  	}
    34  	return h
    35  }
    36  
    37  // refuseToDie hangs indefinitely
    38  func refuseToDie(c hiera.ProviderContext) dgo.Map {
    39  	x := make(chan bool)
    40  	<-x
    41  	return nil
    42  }
    43  
    44  // panicAttack panics with an error
    45  func panicAttack(c hiera.ProviderContext) dgo.Map {
    46  	panic(errors.New(`dit dit dit daah daah daah dit dit dit`))
    47  }
    48  
    49  func tfSimulation(c hiera.ProviderContext) dgo.Map {
    50  	var state map[string]interface{}
    51  	err := json.Unmarshal([]byte(`{
    52      "dns_resource_group_name": "cbuk-shared-sharedproduction-dns-uksouth",
    53      "dns_zones": {
    54  			"cbinnovation.uk": {
    55          "id": "/subscriptions/xxx/resourceGroups/cbuk-shared-sharedproduction-dns-uksouth/providers/Microsoft.Network/dnszones/cbinnovation.uk",
    56          "max_number_of_record_sets": 10000,
    57          "name": "cbinnovation.uk",
    58          "name_servers": [
    59            "ns1-04.azure-dns.com.",
    60            "ns2-04.azure-dns.net.",
    61            "ns3-04.azure-dns.org.",
    62            "ns4-04.azure-dns.info."
    63          ],
    64          "number_of_record_sets": 2,
    65          "registration_virtual_network_ids": null,
    66          "resolution_virtual_network_ids": null,
    67          "resource_group_name": "cbuk-shared-sharedproduction-dns-uksouth",
    68          "tags": {
    69            "environment": "sharedproduction",
    70            "organisation": "cbuk",
    71            "system": "shared",
    72            "technology": "dns"
    73          },
    74          "zone_type": "Public"
    75        }
    76      }
    77    }`), &state)
    78  	if err != nil {
    79  		panic(err)
    80  	}
    81  	v := vf.MutableMap()
    82  	for k, os := range state {
    83  		v.Put(k, os)
    84  	}
    85  	return vf.Map(`root`, v)
    86  }