Skip to content

Instantly share code, notes, and snippets.

@daveadams
Created July 14, 2017 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daveadams/7f088e3ac338b54551a5124ad34baed2 to your computer and use it in GitHub Desktop.
Save daveadams/7f088e3ac338b54551a5124ad34baed2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'diplomat'
def die(msg)
STDERR.puts "ERROR: #{msg}"
exit 1
end
def print_usage
STDERR.puts <<USAGE
Usage:
#{$0} create <acl-name> '<acl-policy>'
Creates or updates the ACL named <acl-name> with <acl-policy>.
#{$0} delete <acl-name>
Deletes the ACL named <acl-name> if it exists.
USAGE
exit 1
end
def get_acl(name)
Diplomat::Acl.list.find { |acl| acl["Name"] == name }
end
def create_acl(name, policy)
acl = get_acl(name)
if acl.nil?
# create acl
new_acl = {
"Name" => name,
"Type" => "client",
"Rules" => policy,
}
response = Diplomat::Acl.create(new_acl)
if response["ID"].nil?
die "An error occurred when creating the ACL: #{response}"
end
else
# update acl
acl["Rules"] = policy
response = Diplomat::Acl.update(acl)
if response["ID"].nil?
die "An error occurred when updating the ACL: #{response}"
end
end
end
def delete_acl(name)
acl = get_acl(name)
exit 0 if acl.nil?
if not Diplomat::Acl.destroy(acl["ID"])
die "Could not delete ACL"
end
end
print_usage if ARGV.count < 2
Diplomat.configure do |config|
config.acl_token = ENV['CONSUL_HTTP_TOKEN']
# do other configuration here if necessary
end
case ARGV[0]
when "create"
create_acl(ARGV[1], ARGV[2])
when "delete"
delete_acl(ARGV[1])
else
print_usage
end
@daveadams
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment