Last active
December 9, 2019 16:09
-
-
Save ripienaar/ed41849e0006cfd063132acaea56b2fe to your computer and use it in GitHub Desktop.
generated golang choria client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// equivelant of: | |
// | |
// mco rpc puppet enable -W country=mt | |
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/ripienaar/puppet/puppetclient" | |
) | |
func summarize(r puppetclient.Stats) { | |
nr := r.NoResponseFrom() | |
if len(nr) > 0 { | |
fmt.Printf("No responses received from %d hosts", len(nr)) | |
} | |
} | |
func enable(ctx context.Context) error { | |
// instance of puppet client, panics if fails | |
// uses default puppet config path | |
pc := puppetclient.Must() | |
// call to the network, filtering on all nodes in `mt`, | |
// Enable() has no required or optional inputs, see disable | |
res, err := pc.OptionFactFilter("country=mt").Enable().Do(ctx) | |
if err != nil { | |
return err | |
} | |
// prints all the responses from the network | |
res.EachOutput(func(r *puppetclient.EnableOutput) { | |
if r.ResultDetails().OK() { | |
fmt.Printf("OK: %-40s: enabled: %v\n", r.ResultDetails().Sender(), r.Enabled()) | |
} else { | |
fmt.Printf("!!: %-40s: message: %s\n", r.ResultDetails().Sender(), r.ResultDetails().StatusMessage()) | |
} | |
}) | |
summarize(res.Stats()) | |
return nil | |
} | |
func main() { | |
err := enable(context.Background()) | |
if err != nil { | |
panic(err) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func disable(ctx context.Context) error { | |
pc := puppetclient.Must() | |
// call to the network, filtering on all nodes in `mt`, | |
// Disable() has an optional input `Message` | |
res, err := pc.OptionFactFilter("country=mt").Disable().Message("testing choria").Do(ctx) | |
if err != nil { | |
return err | |
} | |
// show and report as before | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func resource(ctx context.Context) error { | |
pc := puppetclient.Must() | |
// Resource has 2 required inputs and one optional `Environment` input | |
res, err := pc.OptionFactFilter("country=mt").Resource("zsh", "package").Environment("development").Do(ctx) | |
if err != nil { | |
return err | |
} | |
// show and report as before | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment