├── .circleci └── config.yml ├── .gitignore ├── .rspec ├── .rubocop.yml ├── .travis.yml ├── CHANGELOG.md ├── Gemfile ├── LICENSE ├── OWNERS.md ├── README.md ├── Rakefile ├── lib ├── packet.rb ├── packet │ ├── client.rb │ ├── client │ │ ├── devices.rb │ │ ├── facilities.rb │ │ ├── operating_systems.rb │ │ ├── organizations.rb │ │ ├── plans.rb │ │ ├── projects.rb │ │ ├── ssh_keys.rb │ │ └── users.rb │ ├── configuration.rb │ ├── device.rb │ ├── entity.rb │ ├── entity │ │ ├── associations.rb │ │ ├── base.rb │ │ ├── finders.rb │ │ ├── persistence.rb │ │ ├── serialization.rb │ │ └── timestamps.rb │ ├── errors.rb │ ├── facility.rb │ ├── global_id.rb │ ├── operating_system.rb │ ├── organization.rb │ ├── plan.rb │ ├── project.rb │ ├── ssh_key.rb │ ├── user.rb │ └── version.rb └── packethost.rb ├── packethost.gemspec └── spec ├── fixtures ├── operating_systems.json ├── plans.json ├── project.json ├── projects.json ├── ssh_key.json └── ssh_keys.json ├── lib ├── packet │ ├── client_spec.rb │ ├── configuration_spec.rb │ ├── device_spec.rb │ └── project_spec.rb └── packet_spec.rb ├── spec_helper.rb └── support ├── fake_packet.rb ├── shared └── entity.rb └── webmock.rb /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | orbs: 3 | ruby: circleci/ruby@0.1.2 4 | 5 | jobs: 6 | build: 7 | docker: 8 | - image: circleci/ruby:2.6.3-stretch-node 9 | executor: ruby/default 10 | steps: 11 | - checkout 12 | - run: 13 | name: Which bundler? 14 | command: bundle -v 15 | - ruby/bundle-install 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # rcov generated 2 | coverage 3 | coverage.data 4 | 5 | # rdoc generated 6 | rdoc 7 | 8 | # bundler 9 | .bundle 10 | Gemfile.lock 11 | 12 | # jeweler generated 13 | pkg 14 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | AllCops: 2 | Include: 3 | - '**/Rakefile' 4 | - lib/**/* 5 | Exclude: 6 | - Gemfile 7 | 8 | Lint/DuplicateMethods: 9 | Enabled: false 10 | 11 | Metrics/LineLength: 12 | Enabled: false 13 | 14 | Naming/PredicateName: 15 | NamePrefix: 16 | - is_ 17 | - have_ 18 | 19 | Style/Documentation: 20 | Enabled: false 21 | 22 | Style/FormatStringToken: 23 | Enabled: false 24 | 25 | Style/FrozenStringLiteralComment: 26 | Enabled: false 27 | 28 | Style/MissingRespondToMissing: 29 | Enabled: false 30 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.5 4 | - 2.6 5 | - 2.7 6 | env: 7 | - "COVERAGE=true" 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Changelog 2 | ========= 3 | 4 | v0.0.8 5 | ------ 6 | 7 | * [Ruby 2.0 Compatibility](https://github.com/packethost/packet-rb/pull/11) (thanks @kke) 8 | 9 | v0.0.7 10 | ------ 11 | 12 | * [Add power/rescue actions for devices](https://github.com/packethost/packet-rb/pull/9) (thanks again, @kke) 13 | 14 | v0.0.6 15 | ------ 16 | 17 | * [Fix serialization of devices to solve problems with create_device.](https://github.com/packethost/packet-rb/pull/7) (thanks @kke) 18 | 19 | v0.0.5 20 | ------ 21 | 22 | * [Time#parse isn't available unless explicitly required](https://github.com/packethost/packet-rb/pull/5) (thanks @meskyanichi) 23 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /OWNERS.md: -------------------------------------------------------------------------------- 1 | # Owners 2 | 3 | This project is governed by [Equinix Metal] and benefits from a community of users that 4 | collaborate and contribute to its use in Go powered projects, such as the [Equinix Metal 5 | Terraform provider], [Docker machine driver], Kubernetes drivers for [CSI] and [CCM], 6 | the [Equinix Metal CLI], and others. 7 | 8 | See the [packethost/standards glossary] for more details about this file. 9 | 10 | [Equinix Metal]: https://metal.equinix.com 11 | [Equinix Metal Terraform provider]: https://github.com/packethost/terraform-provider-packet 12 | [Docker machine driver]: https://github.com/packethost/docker-machine-driver-packet 13 | [CSI]: https://github.com/packethost/csi-packet 14 | [CCM]: https://github.com/packethost/packet-ccm 15 | [Equinix Metal CLI]: https://github.com/packethost/packet-cli 16 | [packethost/standards 17 | glossary]: https://github.com/packethost/standards/blob/master/glossary.md#ownersmd 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/packethost/packet-rb.svg)](https://travis-ci.org/packethost/packet-rb) ![](https://img.shields.io/badge/Stability-Experimental-red.svg) 2 | 3 | Packet 4 | ====== 5 | 6 | A Ruby client for the Packet API. 7 | 8 | This repository is [Experimental](https://github.com/packethost/standards/blob/master/experimental-statement.md) meaning that it's based on untested ideas or techniques and not yet established or finalized or involves a radically new and innovative style! This means that support is best effort (at best!) and we strongly encourage you to NOT use this in production. 9 | 10 | Installation 11 | ------------ 12 | 13 | Add this to your Gemfile 14 | 15 | gem 'packethost', '~> 0.0.8' 16 | 17 | Or install with 18 | 19 | gem install packethost 20 | 21 | 22 | Configuration 23 | ------------- 24 | 25 | You can either configure the library in a global way (easier): 26 | 27 | Packet.configure do |config| 28 | config.auth_token = 'my_token' 29 | end 30 | 31 | or create and use an individual instance of `Packet::Client` (more complex): 32 | 33 | Packet::Client.new(auth_token) 34 | 35 | Generally speaking, you'll probably want to configure it globally if you only 36 | ever use a single API token. 37 | 38 | Usage 39 | ----- 40 | 41 | If you configured the library globally, you can just call methods on the 42 | `Packet` module. For example: 43 | 44 | Packet.list_projects 45 | => [#, #] 46 | 47 | If you configured a `Packet::Client` manually, you can call those same methods 48 | on the client itself: 49 | 50 | client = Packet::Client.new( ... ) 51 | client.list_projects 52 | => [#, #] 53 | 54 | See a [list of available methods](https://github.com/packethost/packet-rb/tree/master/lib/packet/client). 55 | 56 | Contributing 57 | ------------ 58 | 59 | * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet. 60 | * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it. 61 | * Fork the project. 62 | * Start a feature/bugfix branch. 63 | * Commit and push until you are happy with your contribution. 64 | * Make sure to add tests for it. This is important so we don't break it in a future version unintentionally. You can run the test suite with `bundle exec rake`. 65 | * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so we can cherry-pick around it. 66 | 67 | Copyright 68 | --------- 69 | 70 | Copyright (c) 2015 Packet Host. See `LICENSE` for further details. 71 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'bundler' 3 | require 'bundler/gem_tasks' 4 | 5 | begin 6 | Bundler.setup(:default, :development) 7 | rescue Bundler::BundlerError => e 8 | warn e.message 9 | warn 'Run `bundle install` to install missing gems' 10 | exit e.status_code 11 | end 12 | 13 | require 'rspec/core' 14 | require 'rspec/core/rake_task' 15 | RSpec::Core::RakeTask.new(:spec) do |spec| 16 | spec.pattern = FileList['spec/**/*_spec.rb'] 17 | end 18 | 19 | require 'rubocop/rake_task' 20 | RuboCop::RakeTask.new 21 | 22 | desc 'Run the CI suite' 23 | task ci: %i[spec rubocop] 24 | 25 | task default: :ci 26 | 27 | require 'rdoc/task' 28 | Rake::RDocTask.new do |rdoc| 29 | version = File.exist?('VERSION') ? File.read('VERSION') : '' 30 | 31 | rdoc.rdoc_dir = 'rdoc' 32 | rdoc.title = "packet #{version}" 33 | rdoc.rdoc_files.include('README*') 34 | rdoc.rdoc_files.include('lib/**/*.rb') 35 | end 36 | -------------------------------------------------------------------------------- /lib/packet.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.dirname(__FILE__) 2 | 3 | require 'packet/client' 4 | require 'packet/configuration' 5 | require 'packet/errors' 6 | require 'packet/version' 7 | 8 | module Packet 9 | def self.configuration 10 | @configuration ||= Configuration.new 11 | end 12 | 13 | def self.configure 14 | yield(configuration) 15 | reset_client! 16 | true 17 | end 18 | 19 | def self.reset! 20 | @configuration = Configuration.new 21 | reset_client! 22 | end 23 | 24 | def self.client 25 | @client ||= Packet::Client.new 26 | end 27 | 28 | def self.reset_client! 29 | @client = nil 30 | end 31 | private_class_method :reset_client! 32 | 33 | if RUBY_VERSION >= '1.9' 34 | def self.respond_to_missing?(method_name, include_private = false) 35 | client.respond_to?(method_name, include_private) 36 | end 37 | else 38 | def self.respond_to?(method_name, include_private = false) 39 | client.respond_to?(method_name, include_private) || super 40 | end 41 | end 42 | 43 | def self.method_missing(method_name, *args, &block) 44 | if client.respond_to?(method_name) 45 | client.send(method_name, *args, &block) 46 | else 47 | super 48 | end 49 | end 50 | private_class_method :method_missing 51 | end 52 | -------------------------------------------------------------------------------- /lib/packet/client.rb: -------------------------------------------------------------------------------- 1 | require 'faraday' 2 | require 'faraday_middleware' 3 | 4 | require 'packet/entity' 5 | 6 | %w[device facility operating_system organization plan project ssh_key user].each do |f| 7 | require "packet/#{f}" 8 | require "packet/client/#{f.pluralize}" 9 | end 10 | 11 | module Packet 12 | class Client 13 | attr_accessor :auth_token, :consumer_token, :url 14 | 15 | def initialize(auth_token = nil, consumer_token = nil, url = nil) 16 | self.url = url || Packet.configuration.url 17 | self.auth_token = auth_token || Packet.configuration.auth_token 18 | self.consumer_token = consumer_token || Packet.configuration.consumer_token 19 | end 20 | 21 | %i[get post patch head delete].each do |method| 22 | define_method(method) do |*args| 23 | response = client.send(method, *args) 24 | fail_on_error(response) || response 25 | end 26 | end 27 | 28 | def inspect 29 | %(#<#{self.class}:#{format('%014x', (object_id << 1))}>) 30 | end 31 | 32 | private 33 | 34 | def client 35 | @client ||= Faraday.new(url: url, headers: headers, ssl: { verify: true }) do |faraday| 36 | faraday.request :json 37 | faraday.response :json, content_type: /\bjson$/ 38 | faraday.adapter Faraday.default_adapter 39 | end 40 | end 41 | 42 | def headers 43 | { 44 | 'X-Consumer-Token' => consumer_token, 45 | 'X-Auth-Token' => auth_token, 46 | # 'X-Packet-Staff' => '1', 47 | 'Content-Type' => 'application/json', 48 | 'Accept' => 'application/json' 49 | }.reject { |_, v| v.nil? } 50 | end 51 | 52 | def fail_on_error(response) 53 | return if response.success? 54 | 55 | klass = case response.status 56 | when 404 then NotFound 57 | else Error 58 | end 59 | raise klass, response.body 60 | end 61 | 62 | include Packet::Client::Devices 63 | include Packet::Client::Facilities 64 | include Packet::Client::OperatingSystems 65 | include Packet::Client::Organizations 66 | include Packet::Client::Plans 67 | include Packet::Client::Projects 68 | include Packet::Client::SshKeys 69 | include Packet::Client::Users 70 | end 71 | end 72 | -------------------------------------------------------------------------------- /lib/packet/client/devices.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Client 3 | module Devices 4 | def list_devices(project_id, *args) 5 | get("projects/#{project_id}/devices", *args).body['devices'].map { |p| Packet::Device.new(p, self) } 6 | end 7 | 8 | def get_device(id, *args) 9 | Packet::Device.new(get("devices/#{id}", *args).body, self) 10 | end 11 | 12 | def create_device(device) 13 | post("projects/#{device.project_id}/devices", device.to_hash).tap do |response| 14 | device.update_attributes(response.body) 15 | end 16 | end 17 | 18 | def update_device(device) 19 | patch("devices/#{device.id}", device.to_hash).tap do |response| 20 | device.update_attributes(response.body) 21 | end 22 | end 23 | 24 | def reboot_device(device) 25 | action(device, 'reboot') 26 | end 27 | 28 | def rescue_device(device) 29 | action(device, 'rescue') 30 | end 31 | 32 | def power_on_device(device) 33 | action(device, 'power_on') 34 | end 35 | 36 | def power_off_device(device) 37 | action(device, 'power_off') 38 | end 39 | 40 | def delete_device(device_or_id) 41 | id = if device_or_id.is_a?(Packet::Device) 42 | device_or_id.id 43 | else 44 | device_or_id 45 | end 46 | delete("devices/#{id}") 47 | end 48 | 49 | private 50 | 51 | def action(device, action_type) 52 | post("devices/#{device.id}/actions", type: action_type).success? 53 | end 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /lib/packet/client/facilities.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Client 3 | module Facilities 4 | def list_facilities(*args) 5 | get('facilities', *args).body['facilities'].map { |p| Packet::Facility.new(p, self) } 6 | end 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/packet/client/operating_systems.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Client 3 | module OperatingSystems 4 | def list_operating_systems(*args) 5 | get('operating-systems', *args).body['operating_systems'].map { |p| Packet::OperatingSystem.new(p, self) } 6 | end 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/packet/client/organizations.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Client 3 | module Organizations 4 | def list_organizations(*args) 5 | get('organizations', *args).body['organizations'].map { |p| Packet::Organization.new(p, self) } 6 | end 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/packet/client/plans.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Client 3 | module Plans 4 | def list_plans(*args) 5 | get('plans', *args).body['plans'].map { |p| Packet::Plan.new(p, self) } 6 | end 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/packet/client/projects.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Client 3 | module Projects 4 | def list_projects(*args) 5 | get('projects', *args).body['projects'].map { |p| Packet::Project.new(p, self) } 6 | end 7 | 8 | def get_project(id, *args) 9 | Packet::Project.new(get("projects/#{id}", *args).body, self) 10 | end 11 | 12 | def create_project(project) 13 | post('projects', project.to_hash).tap do |response| 14 | project.update_attributes(response.body) 15 | end 16 | end 17 | 18 | def update_project(project) 19 | patch("projects/#{project.id}", project.to_hash).tap do |response| 20 | project.update_attributes(response.body) 21 | end 22 | end 23 | 24 | def delete_project(project_or_id) 25 | id = if project_or_id.is_a?(Packet::Project) 26 | project_or_id.id 27 | else 28 | project_or_id 29 | end 30 | delete("projects/#{id}") 31 | end 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/packet/client/ssh_keys.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Client 3 | module SshKeys 4 | def list_ssh_keys(*args) 5 | get('ssh-keys', *args).body['ssh_keys'].map { |p| Packet::SshKey.new(p, self) } 6 | end 7 | 8 | def get_ssh_key(id, *args) 9 | Packet::SshKey.new(get("ssh-keys/#{id}", *args).body, self) 10 | end 11 | 12 | def create_ssh_key(ssh_key) 13 | post('ssh-keys', ssh_key.to_hash).tap do |response| 14 | ssh_key.update_attributes(response.body) 15 | end 16 | end 17 | 18 | def update_ssh_key(ssh_key) 19 | patch("ssh-keys/#{ssh_key.id}", ssh_key.to_hash).tap do |response| 20 | ssh_key.update_attributes(response.body) 21 | end 22 | end 23 | 24 | def delete_ssh_key(ssh_key_or_id) 25 | id = if ssh_key_or_id.is_a?(Packet::SshKey) 26 | ssh_key_or_id.id 27 | else 28 | ssh_key_or_id 29 | end 30 | delete("ssh-keys/#{id}") 31 | end 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/packet/client/users.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Client 3 | module Users 4 | def get_user(id, *args) 5 | Packet::User.new(get("users/#{id}", *args).body, self) 6 | end 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/packet/configuration.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Configuration 3 | attr_accessor :url, :auth_token, :consumer_token 4 | 5 | def initialize 6 | @url = 'https://api.packet.net'.freeze 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/packet/device.rb: -------------------------------------------------------------------------------- 1 | require 'active_support/core_ext/object/try' 2 | 3 | module Packet 4 | class Device 5 | include Entity 6 | 7 | attr_accessor :hostname, :iqn, :ip_addresses, :state, :tags, :userdata, :project_id, :root_password 8 | has_one :operating_system 9 | has_one :plan 10 | has_one :facility 11 | has_one :project 12 | has_timestamps 13 | 14 | def tags 15 | (@tags ||= []).map(&:to_sym) 16 | end 17 | 18 | def ip_addresses 19 | @ip_addresses ||= [] 20 | end 21 | 22 | %i[provisioning powering_on active powering_off inactive rebooting].each do |s| 23 | define_method(:"#{s}?") { state == s } 24 | end 25 | 26 | def state 27 | @state.try(:to_sym) 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /lib/packet/entity.rb: -------------------------------------------------------------------------------- 1 | require 'active_support/concern' 2 | require 'packet/entity/base' 3 | require 'packet/entity/associations' 4 | require 'packet/entity/finders' 5 | require 'packet/entity/persistence' 6 | require 'packet/entity/serialization' 7 | require 'packet/entity/timestamps' 8 | 9 | module Packet 10 | module Entity 11 | extend ActiveSupport::Concern 12 | 13 | included do 14 | include(Base) 15 | include(Associations) 16 | include(Finders) 17 | include(Persistence) 18 | include(Serialization) 19 | include(Timestamps) 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/packet/entity/associations.rb: -------------------------------------------------------------------------------- 1 | require 'active_support/inflector' 2 | require 'active_support/concern' 3 | 4 | module Packet 5 | module Entity 6 | module Associations 7 | extend ActiveSupport::Concern 8 | 9 | module ClassMethods 10 | def has_one(association) 11 | require "packet/#{association}" 12 | casts(association, lambda do |value| 13 | klass = "Packet::#{association.to_s.classify}".constantize 14 | klass.new(value, client) 15 | end) 16 | end 17 | 18 | def has_many(association) 19 | require "packet/#{association.to_s.singularize}" 20 | casts(association, lambda do |value| 21 | klass = "Packet::#{association.to_s.classify}".constantize 22 | value.map { |v| klass.new(v, client) } 23 | end) 24 | end 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/packet/entity/base.rb: -------------------------------------------------------------------------------- 1 | require 'active_support/inflector' 2 | require 'active_support/concern' 3 | require 'active_support/core_ext/class/attribute' 4 | require 'active_support/core_ext/module/delegation' 5 | 6 | module Packet 7 | module Entity 8 | module Base 9 | extend ActiveSupport::Concern 10 | 11 | included do 12 | class_attribute :_casts 13 | self._casts = {} 14 | 15 | attr_accessor :id 16 | attr_writer :client 17 | delegate :resource_name, to: :class 18 | end 19 | 20 | def initialize(attributes = {}, client = nil) 21 | self.client = client 22 | update_attributes(attributes) 23 | end 24 | 25 | def update_attributes(attributes = {}) 26 | attributes.each_pair do |attribute, value| 27 | setter = "#{attribute}=" 28 | send(setter, _cast_value(attribute, value)) if respond_to?(setter) 29 | end 30 | end 31 | 32 | def client 33 | @client || Packet.client 34 | end 35 | 36 | module ClassMethods 37 | def casts(attribute, transformer) 38 | _casts[attribute.to_sym] = transformer 39 | attr_accessor attribute.to_sym 40 | end 41 | 42 | def resource_name 43 | to_s.demodulize.underscore 44 | end 45 | end 46 | 47 | private 48 | 49 | def _cast_value(attribute, value) 50 | if self.class._casts.key?(attribute.to_sym) 51 | callback = self.class._casts[attribute.to_sym] 52 | instance_exec(value, &callback) 53 | else 54 | value 55 | end 56 | end 57 | end 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /lib/packet/entity/finders.rb: -------------------------------------------------------------------------------- 1 | require 'active_support/inflector' 2 | require 'active_support/concern' 3 | 4 | module Packet 5 | module Entity 6 | module Finders 7 | extend ActiveSupport::Concern 8 | 9 | module ClassMethods 10 | def all(params = {}, client = nil) 11 | (client || Packet.client).send(:"list_#{resource_name.pluralize}", params) 12 | end 13 | 14 | def find(id, params = {}, client = nil) 15 | (client || Packet.client).send(:"get_#{resource_name}", id, params) 16 | end 17 | end 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/packet/entity/persistence.rb: -------------------------------------------------------------------------------- 1 | require 'active_support/concern' 2 | 3 | module Packet 4 | module Entity 5 | module Persistence 6 | extend ActiveSupport::Concern 7 | 8 | def save! 9 | if id 10 | client.send("update_#{resource_name}", self) 11 | else 12 | client.send("create_#{resource_name}", self) 13 | end 14 | true 15 | end 16 | 17 | def destroy! 18 | client.send("delete_#{resource_name}", self) 19 | true 20 | end 21 | 22 | module ClassMethods 23 | def create!(attributes = {}, client = nil) 24 | new(attributes, client).tap(&:save!) 25 | end 26 | 27 | def destroy!(id, client = nil) 28 | (client || Packet.client).send("delete_#{resource_name}", id) 29 | true 30 | end 31 | end 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/packet/entity/serialization.rb: -------------------------------------------------------------------------------- 1 | require 'active_support/concern' 2 | 3 | module Packet 4 | module Entity 5 | module Serialization 6 | extend ActiveSupport::Concern 7 | 8 | class_methods do 9 | def serializer_key(value) 10 | define_method :to_value do 11 | instance_variable_get "@#{value}" 12 | end 13 | end 14 | end 15 | 16 | def to_hash 17 | Hash[ 18 | *instance_variables.reject { |ivar| [:@client].include?(ivar) }.flat_map do |ivar| 19 | ival = instance_variable_get(ivar) 20 | [ivar.to_s.tr('@', ''), ival.respond_to?(:to_value) ? ival.to_value : instance_variable_get(ivar)] 21 | end 22 | ] 23 | end 24 | 25 | alias to_h to_hash 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/packet/entity/timestamps.rb: -------------------------------------------------------------------------------- 1 | require 'time' 2 | require 'active_support/concern' 3 | 4 | module Packet 5 | module Entity 6 | module Timestamps 7 | extend ActiveSupport::Concern 8 | 9 | DEFAULT_TIMESTAMPS = %i[created_at updated_at].freeze 10 | 11 | module ClassMethods 12 | def has_timestamps(timestamps = DEFAULT_TIMESTAMPS) 13 | timestamps.each { |timestamp| casts(timestamp, ->(value) { Time.parse(value) }) } 14 | end 15 | end 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/packet/errors.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Error < StandardError; end 3 | class NotFound < Error; end 4 | end 5 | -------------------------------------------------------------------------------- /lib/packet/facility.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Facility 3 | include Entity 4 | 5 | attr_accessor :name 6 | attr_accessor :code 7 | attr_accessor :features 8 | 9 | serializer_key :code 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /lib/packet/global_id.rb: -------------------------------------------------------------------------------- 1 | require 'active_support/inflector' 2 | 3 | module Packet 4 | class GlobalIDLocator 5 | CLASS_MAP = { 6 | 'Instance' => :device 7 | }.freeze 8 | 9 | def initialize(client) 10 | @client = client || Packet::Client.instance 11 | end 12 | 13 | def locate(gid) 14 | @client.send method_for_model_name(gid.model_name), gid.model_id 15 | end 16 | 17 | private 18 | 19 | def method_for_model_name(name) 20 | if CLASS_MAP.key?(name) 21 | CLASS_MAP[name] 22 | else 23 | name.underscore 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/packet/operating_system.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class OperatingSystem 3 | include Entity 4 | 5 | attr_accessor :slug 6 | attr_accessor :name 7 | attr_accessor :distro 8 | attr_accessor :version 9 | attr_accessor :provisionable_on 10 | 11 | serializer_key :slug 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/packet/organization.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Organization 3 | include Entity 4 | 5 | attr_accessor :name 6 | attr_accessor :description 7 | attr_accessor :website 8 | attr_accessor :twitter 9 | attr_accessor :tax_id 10 | attr_accessor :main_phone 11 | attr_accessor :billing_phone 12 | attr_accessor :credit_amount 13 | attr_accessor :personal 14 | attr_accessor :customdata 15 | attr_accessor :attn 16 | attr_accessor :purchase_order 17 | attr_accessor :billing_name 18 | attr_accessor :short_id 19 | attr_accessor :account_id 20 | attr_accessor :default_payment_method 21 | attr_accessor :logo_url 22 | attr_accessor :enabled_features 23 | attr_accessor :maintenance_email 24 | attr_accessor :abuse_email 25 | 26 | serializer_key :id 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/packet/plan.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Plan 3 | include Entity 4 | 5 | attr_accessor :slug 6 | attr_accessor :name 7 | attr_accessor :description 8 | attr_accessor :line 9 | attr_accessor :specs 10 | attr_accessor :pricing 11 | 12 | serializer_key :slug 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/packet/project.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class Project 3 | include Entity 4 | 5 | attr_accessor :name 6 | 7 | has_many :devices 8 | has_many :ssh_keys 9 | has_timestamps 10 | 11 | def new_device(opts = {}) 12 | Packet::Device.new(opts.merge(project_id: id)) 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/packet/ssh_key.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class SshKey 3 | include Entity 4 | 5 | attr_accessor :label 6 | attr_accessor :key 7 | 8 | serializer_key :id 9 | 10 | has_timestamps 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /lib/packet/user.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | class User 3 | include Entity 4 | 5 | attr_accessor :first_name 6 | attr_accessor :last_name 7 | # attr_accessor :full_name 8 | attr_accessor :email 9 | attr_accessor :avatar_url 10 | attr_accessor :timezone 11 | 12 | has_timestamps 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/packet/version.rb: -------------------------------------------------------------------------------- 1 | module Packet 2 | VERSION = '0.0.8'.freeze 3 | end 4 | -------------------------------------------------------------------------------- /lib/packethost.rb: -------------------------------------------------------------------------------- 1 | require 'packet' 2 | -------------------------------------------------------------------------------- /packethost.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | lib = File.expand_path('../lib', __FILE__) 4 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 5 | require 'packet/version' 6 | 7 | Gem::Specification.new do |spec| 8 | spec.name = 'packethost' 9 | spec.version = Packet::VERSION 10 | spec.authors = ['Jake Bell', 'Emiliano Jankowski', 'Andrew Hodges'] 11 | spec.email = %w[jake@packet.net emiliano@packet.net andy@packet.net] 12 | 13 | spec.summary = 'Ruby client for the Packet API' 14 | spec.description = 'Ruby client for the Packet API' 15 | spec.homepage = 'https://www.packet.net' 16 | 17 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 18 | spec.require_paths = %w[lib] 19 | spec.bindir = 'bin' 20 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 21 | 22 | spec.add_dependency 'activesupport', '> 4.2', '< 6.2' 23 | spec.add_dependency 'faraday', '>= 0.9.0' 24 | spec.add_dependency 'faraday_middleware', '>= 0.9.0' 25 | 26 | spec.add_development_dependency 'rake', '< 14' 27 | spec.add_development_dependency 'rspec', '~> 3' 28 | spec.add_development_dependency 'webmock', '> 1.20', '< 4' 29 | spec.add_development_dependency 'rdoc', '> 4', '< 7' 30 | spec.add_development_dependency 'bundler' 31 | spec.add_development_dependency 'simplecov', '~> 0' 32 | spec.add_development_dependency 'rubocop', '~> 0.66' 33 | spec.add_development_dependency 'sinatra', '> 1.4', '<3' 34 | spec.add_development_dependency 'pry' 35 | end 36 | -------------------------------------------------------------------------------- /spec/fixtures/operating_systems.json: -------------------------------------------------------------------------------- 1 | { 2 | "operating_systems": [ 3 | { 4 | "slug": "centos_7", 5 | "name": "CentOS 7", 6 | "distro": "centos", 7 | "version": "7", 8 | "provisionable_on": [ 9 | "baremetal_1", 10 | "baremetal_3" 11 | ] 12 | }, 13 | { 14 | "slug": "centos_6", 15 | "name": "CentOS 6", 16 | "distro": "centos", 17 | "version": "6", 18 | "provisionable_on": [ 19 | "baremetal_1", 20 | "baremetal_3" 21 | ] 22 | }, 23 | { 24 | "slug": "coreos_stable", 25 | "name": "CoreOS (stable)", 26 | "distro": "coreos", 27 | "version": "stable", 28 | "provisionable_on": [ 29 | "baremetal_1" 30 | ] 31 | }, 32 | { 33 | "slug": "coreos_beta", 34 | "name": "CoreOS (beta)", 35 | "distro": "coreos", 36 | "version": "beta", 37 | "provisionable_on": [ 38 | "baremetal_1" 39 | ] 40 | }, 41 | { 42 | "slug": "coreos_alpha", 43 | "name": "CoreOS (alpha)", 44 | "distro": "coreos", 45 | "version": "alpha", 46 | "provisionable_on": [ 47 | "baremetal_1", 48 | "baremetal_3" 49 | ] 50 | }, 51 | { 52 | "slug": "debian_8", 53 | "name": "Debian 8", 54 | "distro": "debian", 55 | "version": "8", 56 | "provisionable_on": [ 57 | "baremetal_1", 58 | "baremetal_0", 59 | "baremetal_3" 60 | ] 61 | }, 62 | { 63 | "slug": "debian_7", 64 | "name": "Debian 7", 65 | "distro": "debian", 66 | "version": "7", 67 | "provisionable_on": [ 68 | "baremetal_1" 69 | ] 70 | }, 71 | { 72 | "slug": "ubuntu_14_04", 73 | "name": "Ubuntu 14.04 LTS", 74 | "distro": "ubuntu", 75 | "version": "14.04", 76 | "provisionable_on": [ 77 | "baremetal_1", 78 | "baremetal_0", 79 | "baremetal_3" 80 | ] 81 | } 82 | ] 83 | } 84 | -------------------------------------------------------------------------------- /spec/fixtures/plans.json: -------------------------------------------------------------------------------- 1 | { 2 | "plans": [ 3 | { 4 | "id": "e69c0169-4726-46ea-98f1-939c9e8a3607", 5 | "slug": "baremetal_0", 6 | "name": "Type 0 (Beta)", 7 | "description": "Our Type 0 configuration is a general use \"cloud killer\" server, with a Intel Atom 2.4Ghz processor and 8GB of RAM.", 8 | "line": "baremetal", 9 | "specs": { 10 | "cpus": [ 11 | { 12 | "count": 1, 13 | "type": "Intel Atom C2550 @ 2.4Ghz" 14 | } 15 | ], 16 | "memory": { 17 | "total": "8GB" 18 | }, 19 | "drives": [ 20 | { 21 | "count": 1, 22 | "size": "80GB", 23 | "type": "SSD" 24 | } 25 | ], 26 | "nics": [ 27 | { 28 | "count": 2, 29 | "type": "1Gbps" 30 | } 31 | ], 32 | "features": { 33 | "raid": false, 34 | "txt": true 35 | } 36 | }, 37 | "pricing": { 38 | "hour": 0.1 39 | } 40 | }, 41 | { 42 | "id": "6d1f1ffa-7912-4b78-b50d-88cc7c8ab40f", 43 | "slug": "baremetal_1", 44 | "name": "Type 1", 45 | "description": "Our Type 1 configuration is a zippy general use server, with an Intel E3-1240 v3 processor and 32GB of RAM.", 46 | "line": "baremetal", 47 | "specs": { 48 | "cpus": [ 49 | { 50 | "count": 1, 51 | "type": "Intel E3-1240 v3" 52 | } 53 | ], 54 | "memory": { 55 | "total": "32GB" 56 | }, 57 | "drives": [ 58 | { 59 | "count": 2, 60 | "size": "120GB", 61 | "type": "SSD" 62 | } 63 | ], 64 | "nics": [ 65 | { 66 | "count": 2, 67 | "type": "1Gbps" 68 | } 69 | ], 70 | "features": { 71 | "raid": true, 72 | "txt": true 73 | } 74 | }, 75 | "pricing": { 76 | "hour": 0.4 77 | } 78 | }, 79 | { 80 | "id": "741f3afb-bb2f-4694-93a0-fcbad7cd5e78", 81 | "slug": "baremetal_3", 82 | "name": "Type 3", 83 | "description": "Our Type 3 configuration is a high core, high IO server, with dual Intel E5-2640 v3 processors, 128GB of DDR4 RAM and ultra fast NVME flash drives.", 84 | "line": "baremetal", 85 | "specs": { 86 | "cpus": [ 87 | { 88 | "count": 2, 89 | "type": "Intel E5-2640 v3" 90 | } 91 | ], 92 | "memory": { 93 | "total": "128GB" 94 | }, 95 | "drives": [ 96 | { 97 | "count": 2, 98 | "size": "120GB", 99 | "type": "SSD" 100 | }, 101 | { 102 | "count": 2, 103 | "size": "800GB", 104 | "type": "NVME" 105 | } 106 | ], 107 | "nics": [ 108 | { 109 | "count": 2, 110 | "type": "10Gbps" 111 | } 112 | ], 113 | "features": { 114 | "raid": true, 115 | "txt": true 116 | } 117 | }, 118 | "pricing": { 119 | "hour": 1.75 120 | } 121 | } 122 | ] 123 | } 124 | -------------------------------------------------------------------------------- /spec/fixtures/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "28e41feb-9f9f-421a-8f00-dcb75dbbb0b3", 3 | "name": "First Project", 4 | "credit_amount": 0, 5 | "created_at": "2015-03-20T16:15:23Z", 6 | "updated_at": "2015-10-01T16:52:08Z", 7 | "max_devices": { 8 | "baremetal_0": null, 9 | "baremetal_1": null, 10 | "baremetal_3": null 11 | }, 12 | "members": [ 13 | { 14 | "href": "/users/0e338778-447d-48a4-a9fc-a0ffdde2e1d7" 15 | }, 16 | { 17 | "href": "/users/0f6c6a41-c9ef-4289-aa16-aa63eacf2c47" 18 | } 19 | ], 20 | "memberships": [ 21 | { 22 | "href": "/memberships/e018e2b0-33b2-44c1-82ed-44f9664d7391" 23 | }, 24 | { 25 | "href": "/memberships/2f5d944a-d848-410b-ab36-a58584e5cafe" 26 | } 27 | ], 28 | "invitations": [], 29 | "payment_method": null, 30 | "devices": [ 31 | { 32 | "href": "/devices/840fd8bb-d4e6-4d30-a051-5848f0047589" 33 | }, 34 | { 35 | "href": "/devices/c9ae2725-430c-43d2-b3ee-7914d8de9852" 36 | }, 37 | { 38 | "href": "/devices/64669c44-0818-4c12-bfb7-1753404dc932" 39 | } 40 | ], 41 | "ssh_keys": [ 42 | { 43 | "href": "/ssh-keys/a019c04e-5c77-4161-af42-009be135212f" 44 | }, 45 | { 46 | "href": "/ssh-keys/f521e117-b21c-465e-9054-81f46641f6e8" 47 | } 48 | ], 49 | "href": "/projects/28e41feb-9f9f-421a-8f00-dcb75dbbb0b3" 50 | } 51 | -------------------------------------------------------------------------------- /spec/fixtures/projects.json: -------------------------------------------------------------------------------- 1 | { 2 | "projects": [ 3 | { 4 | "id": "28e41feb-9f9f-421a-8f00-dcb75dbbb0b3", 5 | "name": "First Project", 6 | "credit_amount": 0, 7 | "created_at": "2015-03-20T16:15:23Z", 8 | "updated_at": "2015-10-01T16:52:08Z", 9 | "max_devices": { 10 | "baremetal_0": null, 11 | "baremetal_1": null, 12 | "baremetal_3": null 13 | }, 14 | "members": [ 15 | { 16 | "href": "/users/0e338778-447d-48a4-a9fc-a0ffdde2e1d7" 17 | }, 18 | { 19 | "href": "/users/0f6c6a41-c9ef-4289-aa16-aa63eacf2c47" 20 | } 21 | ], 22 | "memberships": [ 23 | { 24 | "href": "/memberships/e018e2b0-33b2-44c1-82ed-44f9664d7391" 25 | }, 26 | { 27 | "href": "/memberships/2f5d944a-d848-410b-ab36-a58584e5cafe" 28 | } 29 | ], 30 | "invitations": [], 31 | "payment_method": null, 32 | "devices": [ 33 | { 34 | "href": "/devices/840fd8bb-d4e6-4d30-a051-5848f0047589" 35 | } 36 | ], 37 | "ssh_keys": [ 38 | { 39 | "href": "/ssh-keys/a019c04e-5c77-4161-af42-009be135212f" 40 | } 41 | ], 42 | "href": "/projects/28e41feb-9f9f-421a-8f00-dcb75dbbb0b3" 43 | }, 44 | { 45 | "id": "41f1eae7-f86e-4956-ab5e-58ecb3fc09b2", 46 | "name": "Second Project", 47 | "credit_amount": 0, 48 | "created_at": "2015-03-30T16:23:57Z", 49 | "updated_at": "2015-03-30T16:23:57Z", 50 | "max_devices": { 51 | "baremetal_0": null, 52 | "baremetal_1": null, 53 | "baremetal_3": null 54 | }, 55 | "members": [ 56 | { 57 | "href": "/users/00673daf-5a30-4458-8beb-00390b5c2d09" 58 | } 59 | ], 60 | "memberships": [ 61 | { 62 | "href": "/memberships/76170731-e2ed-4a4f-9157-66f291216428" 63 | } 64 | ], 65 | "invitations": [], 66 | "payment_method": null, 67 | "devices": [ 68 | { 69 | "href": "/devices/79877d95-03b4-43ef-85ec-623eb32d3613" 70 | }, 71 | { 72 | "href": "/devices/a24e7e3c-5810-4291-bbe6-3d4f2e867cef" 73 | }, 74 | { 75 | "href": "/devices/dedb4101-c56f-4e0d-8390-8838cb5d8c0f" 76 | } 77 | ], 78 | "ssh_keys": [ 79 | { 80 | "href": "/ssh-keys/a019c04e-5c77-4161-af42-009be135212f" 81 | }, 82 | { 83 | "href": "/ssh-keys/f521e117-b21c-465e-9054-81f46641f6e8" 84 | } 85 | ], 86 | "href": "/projects/41f1eae7-f86e-4956-ab5e-58ecb3fc09b2" 87 | } 88 | ], 89 | "meta": { 90 | "first": { 91 | "href": "/projects?page=1" 92 | }, 93 | "previous": null, 94 | "self": { 95 | "href": "/projects?page=1" 96 | }, 97 | "next": null, 98 | "last": { 99 | "href": "/projects?page=1" 100 | }, 101 | "total": 2 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /spec/fixtures/ssh_key.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "228dd4a2-70d7-42d2-8bb1-e5ba3b2631ff", 3 | "label": "my key", 4 | "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwAKuNRggzc7crhTEeHTQMJ7lTIe3NtbdE/xvSPZBIxfLq8XXDP22Xt0FySMoPR9TyyrwijRysFIrCyE4Zp4EBCH3R7yQrfS+XwR2nSfxdjOpAok9pFK2RftQpMoc0Z8hKl2OgzWkktXfq4gHM8xc1jkCFxgP1qEoxZKuAs1Zjd0Gnwl1Lz36+6y7BbYw61ptQ9S41zjLWG9g6g9IyfoSODtCDiEItcl6IecDPxO/8XgfGmiRuwXUdZ2oN32UNY59f3pV56ozRCfWe0x3Cq55JdWTD9FIUFBgHfuKHPcWZMbZlCRBcjiCOM2S52XGgzlXnRaYdOYE2b6VutDkrea7z user@packet.net", 5 | "fingerprint": "b0:31:aa:bf:b9:67:dd:10:31:82:8f:51:73:3d:47:7c", 6 | "created_at": "2015-03-05T17:44:17Z", 7 | "updated_at": "2015-03-05T17:44:17Z", 8 | "user": { 9 | "href": "/users/687f8ab4-f227-42ba-ab3a-a6c08f2fbded" 10 | }, 11 | "href": "/ssh-keys/228dd4a2-70d7-42d2-8bb1-e5ba3b2631ff" 12 | } -------------------------------------------------------------------------------- /spec/fixtures/ssh_keys.json: -------------------------------------------------------------------------------- 1 | {"ssh_keys":[{"id":"228dd4a2-70d7-42d2-8bb1-e5ba3b2631ff","label":"my key","key":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwAKuNRggzc7crhTEeHTQMJ7lTIe3NtbdE/xvSPZBIxfLq8XXDP22Xt0FySMoPR9TyyrwijRysFIrCyE4Zp4EBCH3R7yQrfS+XwR2nSfxdjOpAok9pFK2RftQpMoc0Z8hKl2OgzWkktXfq4gHM8xc1jkCFxgP1qEoxZKuAs1Zjd0Gnwl1Lz36+6y7BbYw61ptQ9S41zjLWG9g6g9IyfoSODtCDiEItcl6IecDPxO/8XgfGmiRuwXUdZ2oN32UNY59f3pV56ozRCfWe0x3Cq55JdWTD9FIUFBgHfuKHPcWZMbZlCRBcjiCOM2S52XGgzlXnRaYdOYE2b6VutDkrea7z user@packet.net","fingerprint":"b0:31:aa:bf:b9:67:dd:10:31:82:8f:51:73:3d:47:7c","created_at":"2015-03-05T17:44:17Z","updated_at":"2015-03-05T17:44:17Z","user":{"href":"/users/687f8ab4-f227-42ba-ab3a-a6c08f2fbded"},"href":"/ssh-keys/228dd4a2-70d7-42d2-8bb1-e5ba3b2631ff"}]} -------------------------------------------------------------------------------- /spec/lib/packet/client_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | RSpec.describe Packet::Client do 4 | SUPPORTED_HTTP_METHODS = %i[get post patch delete head].freeze 5 | 6 | subject { Packet::Client.new } 7 | let(:faraday) { double(Faraday::Connection) } 8 | let(:not_found_response) { OpenStruct.new(success?: false, status: 404, body: { errors: ['not found'] }) } 9 | let(:error_response) { OpenStruct.new(success?: false, status: 500, body: { errors: ['internal server error'] }) } 10 | before { allow(Faraday).to receive(:new).and_return(faraday) } 11 | before { SUPPORTED_HTTP_METHODS.each { |method| allow(faraday).to receive(method) { response } } } 12 | 13 | describe 'HTTP' do 14 | SUPPORTED_HTTP_METHODS.each do |method| 15 | describe "##{method}" do 16 | context 'when successful' do 17 | let(:response) { OpenStruct.new(success?: true, body: 'return') } 18 | 19 | it 'delegtes to the Faraday connection' do 20 | subject.send(method) 21 | expect(faraday).to have_received(method) 22 | end 23 | 24 | it 'returns the response' do 25 | expect(subject.send(method)).to be(response) 26 | end 27 | end 28 | 29 | context 'when not found' do 30 | let(:response) { not_found_response } 31 | it { expect { subject.send(method) }.to raise_error(Packet::NotFound) } 32 | end 33 | 34 | context 'an error has occurred' do 35 | let(:response) { error_response } 36 | it { expect { subject.send(method) }.to raise_error(Packet::Error) } 37 | end 38 | end 39 | end 40 | end 41 | 42 | describe 'Projects' do 43 | describe '#list_projects' do 44 | let(:count) { rand(1..100) } 45 | let(:response) { OpenStruct.new(success?: true, body: { 'projects' => Array.new(count) { |i| { name: "Project ##{i}" } } }) } 46 | it { expect(subject.list_projects).to be_an(Array) } 47 | it { expect(subject.list_projects).to all(be_a(Packet::Project)) } 48 | it { expect(subject.list_projects.size).to be(count) } 49 | end 50 | 51 | describe '#get_project' do 52 | context 'when found' do 53 | let(:id) { SecureRandom.uuid } 54 | let(:response) { OpenStruct.new(success?: true, body: { id: id }) } 55 | it { expect(subject.get_project(id)).to be_a(Packet::Project) } 56 | it { expect(subject.get_project(id).id).to eq(id) } 57 | 58 | context 'when including nested devices' do 59 | let(:device_id) { SecureRandom.uuid } 60 | let(:response) { OpenStruct.new(success?: true, body: { id: id, devices: [{ id: device_id }] }) } 61 | 62 | it { expect(subject.get_project(id, include: 'devices').devices).to all(be_a(Packet::Device)) } 63 | it { expect(subject.get_project(id, include: 'devices').devices.count).to be(1) } 64 | end 65 | end 66 | 67 | context 'when not found' do 68 | let(:response) { not_found_response } 69 | it { expect { subject.get_project('some other ID') }.to raise_error(Packet::Error) } 70 | end 71 | end 72 | 73 | describe '#create_project' do 74 | let(:id) { SecureRandom.uuid } 75 | let(:project) { Packet::Project.new(name: 'Test Project') } 76 | let(:response) { OpenStruct.new(success?: true, body: { id: id }) } 77 | 78 | it 'calls POST /projects' do 79 | expect(subject).to receive(:post).once.with( 80 | 'projects', 81 | hash_including('name' => 'Test Project') 82 | ).and_return(response) 83 | subject.create_project(project) 84 | end 85 | 86 | context 'when successful' do 87 | it { expect(subject.create_project(project)).to be(response) } 88 | it { expect { subject.create_project(project) }.to change { project.id }.from(nil).to(id) } 89 | end 90 | 91 | context 'when unsuccessful' do 92 | before { allow(subject).to receive(:post).and_raise(Packet::Error) } 93 | 94 | it { expect { subject.create_project(project) }.to raise_error(Packet::Error) } 95 | end 96 | end 97 | 98 | describe '#update_project' do 99 | let(:project) { Packet::Project.new(id: SecureRandom.uuid, name: 'Test Project') } 100 | let(:response) { OpenStruct.new(success?: true, body: { id: project.id, name: 'New Name' }) } 101 | before { project.name = 'New Name' } 102 | 103 | it 'calls PATCH /projects/:id' do 104 | expect(subject).to receive(:patch).once.with( 105 | "projects/#{project.id}", 106 | hash_including('name' => 'New Name') 107 | ).and_return(response) 108 | subject.update_project(project) 109 | end 110 | 111 | context 'when successful' do 112 | it { expect(subject.update_project(project)).to be(response) } 113 | end 114 | 115 | context 'when unsuccessful' do 116 | before { allow(subject).to receive(:patch).and_raise(Packet::Error) } 117 | 118 | it { expect { subject.update_project(project) }.to raise_error(Packet::Error) } 119 | end 120 | end 121 | 122 | describe '#delete_project' do 123 | let(:project) { Packet::Project.new(id: SecureRandom.uuid) } 124 | 125 | it 'calls DELETE /projects/:id' do 126 | expect(subject).to receive(:delete).once.with("projects/#{project.id}") 127 | subject.delete_project(project) 128 | end 129 | 130 | context 'when successful' do 131 | let(:response) { OpenStruct.new(success?: true) } 132 | it { expect(subject.delete_project(project)).to be(response) } 133 | end 134 | 135 | context 'when unsuccessful' do 136 | let(:response) { error_response } 137 | it { expect { subject.delete_project(project) }.to raise_error(Packet::Error) } 138 | end 139 | end 140 | end 141 | 142 | describe 'Devices' do 143 | describe '#list_devices' 144 | describe '#get_device' 145 | describe '#create_device' 146 | describe '#delete_device' 147 | describe '#update_device' 148 | describe '#reboot_device' 149 | describe '#rescue_device' 150 | describe '#power_on_device' 151 | describe '#power_off_device' 152 | end 153 | end 154 | -------------------------------------------------------------------------------- /spec/lib/packet/configuration_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | module Packet 4 | RSpec.describe Configuration do 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /spec/lib/packet/device_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | RSpec.describe Packet::Device do 4 | let(:plan) { Packet::Plan.new(slug: 'test_slug') } 5 | 6 | before do 7 | allow(plan).to receive(:to_value) 8 | end 9 | 10 | subject { Packet::Device.new(plan: plan.to_hash) } 11 | 12 | describe '#to_hash output should have values instead of objects for children' do 13 | it { expect(subject.to_hash['plan']).to be_a(String) } 14 | it { expect(subject.to_hash['plan']).to eq plan.slug } 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /spec/lib/packet/project_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | RSpec.describe Packet::Project do 4 | let(:client) { double(Packet::Client) } 5 | before do 6 | allow(client).to receive(:create_project) 7 | allow(client).to receive(:update_project) 8 | allow(client).to receive(:delete_project) 9 | end 10 | subject { Packet::Project.new({ created_at: '2015-10-10T00:00:00Z', updated_at: '2015-11-11T00:00:00Z' }, client) } 11 | 12 | describe '.all' do 13 | let(:count) { 2 } 14 | subject { Packet::Project.all } 15 | 16 | it { expect(subject).to be_an(Array) } 17 | it { expect(subject).to all(be_a(Packet::Project)) } 18 | it { expect(subject.size).to be(count) } 19 | end 20 | 21 | describe '.find' do 22 | context 'when a project is found' do 23 | let(:id) { '28e41feb-9f9f-421a-8f00-dcb75dbbb0b3' } 24 | subject { Packet::Project.find(id) } 25 | 26 | it { expect(subject).to be_a(Packet::Project) } 27 | it { expect(subject.id).to eq(id) } 28 | end 29 | 30 | context 'when no project is found' do 31 | subject { Packet::Project.find('invalid_project_id') } 32 | it { expect { subject }.to raise_error(Packet::NotFound) } 33 | end 34 | end 35 | 36 | describe '#save!' do 37 | context 'with a new record' do 38 | it 'calls Packet::Client#create_project' do 39 | expect(client).to receive(:create_project).once.with(subject) 40 | subject.save! 41 | end 42 | 43 | context 'when successful' do 44 | it { expect(subject.save!).to be(true) } 45 | end 46 | 47 | context 'when unsuccessful' do 48 | before { allow(client).to receive(:create_project).and_raise(Packet::Error) } 49 | 50 | it { expect { subject.save! }.to raise_error(Packet::Error) } 51 | end 52 | end 53 | 54 | context 'with an existing record' do 55 | before { subject.id = SecureRandom.uuid } 56 | 57 | it 'calls Packet::Client#update_project' do 58 | expect(client).to receive(:update_project).once.with(subject) 59 | subject.save! 60 | end 61 | 62 | context 'when successful' do 63 | it { expect(subject.save!).to be(true) } 64 | end 65 | 66 | context 'when unsuccessful' do 67 | before { allow(client).to receive(:update_project).and_raise(Packet::Error) } 68 | 69 | it { expect { subject.save! }.to raise_error(Packet::Error) } 70 | end 71 | end 72 | end 73 | 74 | describe '#destroy!' do 75 | before { subject.id = SecureRandom.uuid } 76 | 77 | it 'calls Packet::Client#delete_project' do 78 | expect(client).to receive(:delete_project).once.with(subject) 79 | subject.destroy! 80 | end 81 | 82 | context 'when successful' do 83 | it { expect(subject.destroy!).to be(true) } 84 | end 85 | 86 | context 'when unsuccessful' do 87 | before { allow(client).to receive(:delete_project).and_raise(Packet::Error) } 88 | 89 | it { expect { subject.destroy! }.to raise_error(Packet::Error) } 90 | end 91 | end 92 | 93 | describe '#new_device' do 94 | before { subject.id = SecureRandom.uuid } 95 | 96 | it 'creates a Packet::Device object that has a project_id instance variable' do 97 | expect(subject.new_device.project_id).to eq subject.id 98 | end 99 | end 100 | 101 | it_behaves_like 'an entity' 102 | end 103 | -------------------------------------------------------------------------------- /spec/lib/packet_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | RSpec.describe Packet do 4 | describe '.configure' do 5 | it { expect { |probe| Packet.configure(&probe) }.to yield_with_args(Packet::Configuration) } 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'simplecov' 2 | 3 | ENV['COVERAGE'] && SimpleCov.start 4 | $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) 5 | $LOAD_PATH.unshift(File.dirname(__FILE__)) 6 | 7 | require 'rspec' 8 | require 'packet' 9 | 10 | # Requires supporting files with custom matchers and macros, etc, 11 | # in ./support/ and its subdirectories. 12 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } 13 | 14 | RSpec.configure do |config| 15 | # rspec-expectations config goes here. You can use an alternate 16 | # assertion/expectation library such as wrong or the stdlib/minitest 17 | # assertions if you prefer. 18 | config.expect_with :rspec do |expectations| 19 | # This option will default to `true` in RSpec 4. It makes the `description` 20 | # and `failure_message` of custom matchers include text for helper methods 21 | # defined using `chain`, e.g.: 22 | # be_bigger_than(2).and_smaller_than(4).description 23 | # # => "be bigger than 2 and smaller than 4" 24 | # ...rather than: 25 | # # => "be bigger than 2" 26 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 27 | end 28 | 29 | # rspec-mocks config goes here. You can use an alternate test double 30 | # library (such as bogus or mocha) by changing the `mock_with` option here. 31 | config.mock_with :rspec do |mocks| 32 | # Prevents you from mocking or stubbing a method that does not exist on 33 | # a real object. This is generally recommended, and will default to 34 | # `true` in RSpec 4. 35 | mocks.verify_partial_doubles = true 36 | end 37 | 38 | # Limits the available syntax to the non-monkey patched syntax that is recommended. 39 | # For more details, see: 40 | # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax 41 | # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 42 | # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching 43 | config.disable_monkey_patching! 44 | 45 | # Many RSpec users commonly either run the entire suite or an individual 46 | # file, and it's useful to allow more verbose output when running an 47 | # individual spec file. 48 | if config.files_to_run.one? 49 | # Use the documentation formatter for detailed output, 50 | # unless a formatter has already been configured 51 | # (e.g. via a command-line flag). 52 | config.default_formatter = 'doc' 53 | end 54 | 55 | # Print the 10 slowest examples and example groups at the 56 | # end of the spec run, to help surface which specs are running 57 | # particularly slow. 58 | # config.profile_examples = 10 59 | 60 | # Run specs in random order to surface order dependencies. If you find an 61 | # order dependency and want to debug it, you can fix the order by providing 62 | # the seed, which is printed after each run. 63 | # --seed 1234 64 | config.order = :random 65 | 66 | # Seed global randomization in this process using the `--seed` CLI option. 67 | # Setting this allows you to use `--seed` to deterministically reproduce 68 | # test failures related to randomization by passing the same `--seed` value 69 | # as the one that triggered the failure. 70 | Kernel.srand config.seed 71 | end 72 | -------------------------------------------------------------------------------- /spec/support/fake_packet.rb: -------------------------------------------------------------------------------- 1 | require 'sinatra/base' 2 | 3 | class FakePacket < Sinatra::Base 4 | get('/operating-systems') { json_response 200, fixture(:operating_systems) } 5 | get('/plans') { json_response 200, fixture(:plans) } 6 | get('/projects') { json_response 200, fixture(:projects) } 7 | get('/projects/:id') do |id| 8 | projects = JSON.parse(fixture(:projects))['projects'] 9 | 10 | if (project = projects.find { |p| p['id'] == id }) 11 | json_response 200, project.to_json 12 | else 13 | json_response 404, { errors: ['Not found'] }.to_json 14 | end 15 | end 16 | get('/ssh-keys') { json_response 200, fixture(:ssh_keys) } 17 | post('/ssh-keys') { json_response 201, fixture(:ssh_key) } 18 | get('/ssh-keys/:key') { json_response 200, fixture(:ssh_key) } 19 | patch('/ssh-keys/:key') { json_response 200, fixture(:ssh_key) } 20 | delete('/ssh-keys/:key') { json_response 204 } 21 | 22 | private 23 | 24 | def json_response(response_code, body = nil) 25 | content_type :json 26 | status response_code 27 | body if body 28 | end 29 | 30 | def fixture(name) 31 | File.open("#{File.join(File.dirname(__FILE__), '..', 'fixtures', name.to_s)}.json", 'rb').read 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /spec/support/shared/entity.rb: -------------------------------------------------------------------------------- 1 | shared_examples 'an entity' do 2 | before do 3 | subject.update_attributes('created_at' => '2015-03-05T17:44:17Z', 'updated_at' => '2015-03-05T17:44:17Z') 4 | end 5 | 6 | describe '#id' do 7 | it { expect(subject).to respond_to(:id) } 8 | end 9 | 10 | %i[created_at updated_at].each do |timestamp| 11 | describe timestamp do 12 | it { expect(subject).to respond_to(timestamp) } 13 | it { expect(subject.send(timestamp)).to be_a(Time) } 14 | end 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /spec/support/webmock.rb: -------------------------------------------------------------------------------- 1 | require 'webmock/rspec' 2 | 3 | RSpec.configure do |config| 4 | config.before { WebMock.disable_net_connect!(allow_localhost: true) } 5 | 6 | # Stub request through FakePacket 7 | config.before(:each) do 8 | stub_request(:any, /api.packet.net/).to_rack(FakePacket) 9 | end 10 | end 11 | --------------------------------------------------------------------------------