droplet_kit, DropletKit是 ruby的官方 DigitalOcean API客户端

分享于 

18分钟阅读

GitHub

  繁體 雙語
DropletKit is the official Digital Ocean API client for Ruby.
  • 源代码名称:droplet_kit
  • 源代码网址:http://www.github.com/digitalocean/droplet_kit
  • droplet_kit源代码文档
  • droplet_kit源代码下载
  • Git URL:
    git://www.github.com/digitalocean/droplet_kit.git
    Git Clone代码到本地:
    git clone http://www.github.com/digitalocean/droplet_kit
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/digitalocean/droplet_kit
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    DropletKit

    DropletKit是正式的DigitalOcean V2 客户端。 它支持API使用 ruby 编写的简单接口所能做的一切。

    Build Status

    安装

    将此行添加到你的应用程序的Gemfile中:

    
    gem 'droplet_kit'
    
    
    
    

    然后执行:

     
    $ bundle
    
    
    
     

    或者将它的自己安装为:

    
    $ gem install droplet_kit
    
    
    
    

    用法

    你需要在 https://cloud.digitalocean.com/settings/applications的数字 控制面板 中生成一个访问令牌。

    使用访问令牌,用它检索客户端实例。

    client =DropletKit::Client.new(access_token:'YOUR_TOKEN')

    设计

    DropletKit严格按照你的客户端的方法设计资源。 例如对于液滴,你将像这样调用你的客户机:

    client =DropletKit::Client.new(access_token:'YOUR_TOKEN')
    client.droplets #=> DropletsResource

    DropletKit将返回包含由API提供的信息的普通 ruby objects(tm)。 例如:

    client =DropletKit::Client.new(access_token:'YOUR_TOKEN')
    client.droplets.all# => [ DropletKit::Droplet(id: 123, name: 'something.com',.. .), DropletKit::Droplet(id: 1066, name: 'bunk.com',.. .) ]

    当你想保存对象时,你的责任是实例化对象并使用资源对象持久化它们。 让我们使用创建液滴作为示例:

    client =DropletKit::Client.new(access_token:'YOUR_TOKEN')
    droplet =DropletKit::Droplet.new(name:'mysite.com', region:'nyc2', image:'ubuntu-14-04-x64', size:'512mb')
    created = client.droplets.create(droplet)# => DropletKit::Droplet(id: 1231, name: 'something.com',.. .)

    要检索对象,可以在资源( 如果API支持它) 上执行这里类型的操作:

    client =DropletKit::Client.new(access_token:'YOUR_TOKEN')
    droplet = client.droplets.find(id:123)# => DropletKit::Droplet(id: 1231, name: 'something.com',.. .)
    所有资源和操作。

    自签名证书资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.certificates #=> DropletKit::CertificateResource

    支持的操作:

    • client.certificates.find(id: 'id')
    • client.certificates.all()
    • client.certificates.create(certificate)
    • client.certificates.delete(id: 'id')

    液滴资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.droplets #=> DropletKit::DropletResource

    支持的操作:

    • client.droplets.all()
    • client.droplets.all(tag_name: 'tag_name')
    • client.droplets.find(id: 'id')
    • client.droplets.create(droplet)
    • client.droplets.create_multiple(droplet)
    • client.droplets.delete(id: 'id')
    • client.droplets.delete_for_tag(tag_name: 'tag_name')
    • client.droplets.kernels(id: 'id')
    • client.droplets.snapshots(id: 'id')
    • client.droplets.backups(id: 'id')
    • client.droplets.actions(id: 'id')

    液滴动作资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.droplet_actions #=> DropletKit::DropletAction

    支持的操作:

    • client.droplet_actions.reboot(droplet_id: droplet.id)
    • client.droplet_actions.power_cycle(droplet_id: droplet.id)
    • client.droplet_actions.power_cycle_for_tag(tag_name: 'tag_name')
    • client.droplet_actions.shutdown(droplet_id: droplet.id)
    • client.droplet_actions.shutdown_for_tag(tag_name: 'tag_name')
    • client.droplet_actions.power_off(droplet_id: droplet.id)
    • client.droplet_actions.power_off_for_tag(tag_name: 'tag_name')
    • client.droplet_actions.power_on(droplet_id: droplet.id)
    • client.droplet_actions.power_on_for_tag(tag_name: 'tag_name')
    • client.droplet_actions.password_reset(droplet_id: droplet.id)
    • client.droplet_actions.enable_ipv6(droplet_id: droplet.id)
    • client.droplet_actions.enable_ipv6_for_tag(tag_name: 'tag_name')
    • client.droplet_actions.enable_backups(droplet_id: droplet.id)
    • client.droplet_actions.enable_backups_for_tag(tag_name: 'tag_name')
    • client.droplet_actions.disable_backups(droplet_id: droplet.id)
    • client.droplet_actions.disable_backups_for_tag(tag_name: 'tag_name')
    • client.droplet_actions.upgrade(droplet_id: droplet.id)
    • client.droplet_actions.enable_private_networking(droplet_id: droplet.id)
    • client.droplet_actions.enable_private_networking_for_tag(tag_name: 'tag_name')
    • client.droplet_actions.snapshot(droplet_id: droplet.id, name: 'Snapshot Name')
    • client.droplet_actions.snapshot_for_tag(tag_name: 'tag_name', name: 'Snapshot Name')
    • client.droplet_actions.change_kernel(droplet_id: droplet.id, kernel: 'kernel_id')
    • client.droplet_actions.rename(droplet_id: droplet.id, name: 'New-Droplet-Name')
    • client.droplet_actions.rebuild(droplet_id: droplet.id, image: 'image_id')
    • client.droplet_actions.restore(droplet_id: droplet.id, image: 'image_id')
    • client.droplet_actions.resize(droplet_id: droplet.id, size: '1gb')
    • client.droplet_actions.find(droplet_id: droplet.id, id: action.id)
    • client.droplet_actions.action_for_id(droplet_id: droplet.id, type: 'event_name', param: 'value')
    • client.droplet_actions.action_for_tag(tag_name: 'tag_name', type: 'event_name', param: 'value')

    域资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.domains #=> DropletKit::DomainResource

    支持的操作:

    • client.domains.all()
    • client.domains.create(domain)
    • client.domains.find(name: 'name')
    • client.domains.delete(name: 'name')

    域记录资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.domain_records #=> DropletKit::DomainRecordResourcedomain_record =DropletKit::DomainRecord.new(
     type:'CNAME',
     name:'www',
     data:'@',
     ttl:1800)

    支持的操作:

    • client.domain_records.all(for_domain: 'for_domain')
    • client.domain_records.create(domain_record, for_domain: 'for_domain')
    • client.domain_records.find(for_domain: 'for_domain', id: 'id')
    • client.domain_records.delete(for_domain: 'for_domain', id: 'id')
    • client.domain_records.update(domain_record, for_domain: 'for_domain', id: 'id')

    防火墙资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.firewalls #=> DropletKit::FirewallResourceinbound_rule =DropletKit::FirewallInboundRule.new(
     protocol:'icmp',
     ports:'0',
     sources: {
     tags: ['frontend', 'backend'],
     load_balancer_uids: ['d2d3920a-9d45-41b0-b018-d15e18ec60a4']
     }
    )
    outbound_rule =DropletKit::FirewallOutboundRule.new(
     protocol:'icmp',
     ports:'0',
     destinations: {
     addresses: ["127.0.0.0"],
     droplet_ids: [456, 789]
     }
    )
    firewall =DropletKit::Firewall.new(
     name:'firewall',
     inbound_rules: [
     inbound_rule
     ],
     outbound_rules: [
     outbound_rule
     ],
     droplet_ids: [123],
     tags: ['backend']
    )

    支持的操作:

    • client.firewalls.find(id: 'id')
    • client.firewalls.create(firewall)
    • client.firewalls.update(firewall, id: 'id')
    • client.firewalls.all()
    • client.firewalls.all_by_droplet(droplet_id: 'id')
    • client.firewalls.delete(id: 'id')
    • client.firewalls.add_droplets([droplet.id], id: 'id')
    • client.firewalls.remove_droplets([droplet.id], id: 'id')
    • client.firewalls.add_tags([tag.name], id: 'id')
    • client.firewalls.remove_tags([tag.name], id: 'id')
    • client.firewalls.add_rules(inbound_rules: [inbound_rule], outbound_rules: [outbound_rule], id: 'id')
    • client.firewalls.remove_rules(inbound_rules: [inbound_rule], outbound_rules: [outbound_rule], id: 'id')

    图像资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.images #=> DropletKit::ImageResource

    支持的操作:

    • client.images.all()
    • client.images.find(id: 'id')
    • client.images.delete(id: 'id')
    • client.images.update(image, id: 'id')

    图像操作资源

    
    client = DropletKit::Client.new(access_token: 'TOKEN')
    
    
    client.image_actions #=> DropletKit::ImageActionResource
    
    
    
    

    支持的图像操作:

    • client.image_actions.all(image_id: 123)
    • client.image_actions.find(image_id: 123, id: 123455)
    • client.image_actions.transfer(image_id: 123, region: 'nyc3')

    负载平衡器资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.load_balancers #=> DropletKit::LoadBalancerResource

    支持的操作:

    • client.load_balancers.find(id: 'id')
    • client.load_balancers.all()
    • client.load_balancers.create(load_balancer)
    • client.load_balancers.update(load_balancer, id: 'id')
    • client.load_balancers.delete(id: 'id')
    • client.load_balancers.add_droplets([droplet.id], id: 'id')
    • client.load_balancers.remove_droplets([droplet.id], id: 'id')
    • client.load_balancers.add_forwarding_rules([forwarding_rule], id: 'id')
    • client.load_balancers.remove_forwarding_rules([forwarding_rule], id: 'id')

    区域资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.regions #=> DropletKit::RegionResource

    支持的操作:

    • client.regions.all()

    大小资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.sizes #=> DropletKit::SizeResource

    支持的操作:

    • client.sizes.all()

    SSH密钥资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.ssh_keys #=> DropletKit::SSHKeyResource

    当你想使用存储的SSH密钥创建液滴时。

    client =DropletKit::Client.new(access_token:'YOUR_TOKEN')
    my_ssh_keys = client.ssh_keys.all.collect {|key| key.fingerprint}
    droplet =DropletKit::Droplet.new(name:'mysite.com', region:'nyc2', image:'ubuntu-14-04-x64', size:'512mb', ssh_keys: my_ssh_keys)
    created = client.droplets.create(droplet)# => DropletKit::Droplet(id: 1231, name: 'something.com',.. .)

    支持的操作:

    • client.ssh_keys.all()
    • client.ssh_keys.create(ssh_key)
    • client.ssh_keys.find(id: 'id')
    • client.ssh_keys.delete(id: 'id')
    • client.ssh_keys.update(ssh_key, id: 'id')

    标记资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.tags #=> DropletKit::TagResource

    支持的操作:

    • client.tags.all()
    • client.tags.find(name: 'name')
    • client.tags.create(DropletKit::Tag.new(name: 'name'))
    • client.tags.delete(name: 'name')
    • client.tags.tag_resources(name: 'name', resources: [{ resource_id => 'droplet_id', resource_type: 'droplet' }])
    • client.tags.untag_resources(name 'name', resources: [{ resource_id => 'droplet_id', resource_type: 'droplet' }])

    电子邮件帐户资源

    client =DropletKit::Client.new(access_token:'TOKEN')
    client.account #=> DropletKit::AccountResource

    支持的操作:

    • client.account.info()

    浮动IP资源

    
    client = DropletKit::Client.new(access_token: 'TOKEN')
    
    
    client.floating_ips #=> DropletKit::FloatingIpResource
    
    
    
    

    支持的操作:

    • client.floating_ips.all()
    • client.floating_ips.find(ip: 'ip address')
    • client.floating_ips.create(floating_ip)
    • client.floating_ips.delete(ip: 'ip address')

    浮动IP操作资源

    
    client = DropletKit::Client.new(access_token: 'TOKEN')
    
    
    client.floating_ip_actions #=> DropletKit::FloatingIpActionResource
    
    
    
    

    支持的操作:

    • client.floating_ip_actions.assign(ip: floating_ip.ip, droplet_id: droplet.id)
    • client.floating_ip_actions.unassign(ip: floating_ip.ip)

    卷资源

    
    client = DropletKit::Client.new(access_token: 'TOKEN')
    
    
    client.volumes #=> DropletKit::VolumeResource
    
    
    
    

    支持的操作:

    • client.volumes.all()
    • client.volumes.find(id: 'id')
    • client.volumes.create(volume)
    • client.volumes.snapshots(id: 'id')
    • client.volumes.create_snapshot(id: 'id', name: 'snapshot-name')
    • client.volumes.delete(id: 'id')

    卷操作资源

    
    client = DropletKit::Client.new(access_token: 'TOKEN')
    
    
    client.volume_actions #=> DropletKit::VolumeActionResource
    
    
    
    

    支持的操作:

    • client.volume_actions.attach(volume_id: volume.id, droplet_id: droplet.id, region: droplet.region.slug)
    • client.volume_actions.detach(volume_id: volume.id, droplet_id: droplet.id, region: droplet.region.slug)
    • client.volume_actions.resize(volume_id: volume.id, size_gigabytes: 123, region: droplet.region.slug)

    卷资源

    
    client = DropletKit::Client.new(access_token: 'TOKEN')
    
    
    client.snapshots #=> DropletKit::SnapshotResource
    
    
    
    

    支持的操作:

    • client.snapshots.all(resource_type: 'droplet')
    • client.snapshots.find(id: 'id')
    • client.snapshots.delete(id: 'id')

    • fork ( https://github.com/digitalocean/droplet_kit/fork )
    • 创建你的特征分支( git checkout -b my-new-feature )
    • 提交你的更改( git commit -am 'Add some feature' )
    • 推送到分支( git push origin my-new-feature )
    • 创建新的拉请求

    插件发布

    如果你已经从帐户中登录,那么将所有发布的更改添加到变更日志中,如果你已经完成了rubygems的登录,那么如果不继续阅读,请运行 rake release

    在do帐户(。搜索 rubygems ) 上找到密码,使用用户( 运行 gem gem push,它会询问你在防火墙上找到的do和密码的电子邮件) 登录,gem push 命令将失败,忽略。 现在运行 rake release,gem 将被推送到 rubygems。


    API  DIG  Digital  
    相关文章