github.com/lyraproj/hiera@v1.0.0-rc4/lookup/testdata/hieratestplugin/hieratestplugin.go (about)

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