diff -Nru juju-core-1.17.7/debian/changelog juju-core-1.18.0/debian/changelog --- juju-core-1.17.7/debian/changelog 2014-03-28 08:58:44.000000000 +0000 +++ juju-core-1.18.0/debian/changelog 2014-04-07 17:25:00.000000000 +0000 @@ -1,3 +1,12 @@ +juju-core (1.18.0-0ubuntu1) trusty; urgency=medium + + * New upstream release (LP: #1287147), including fixes for: + - maas/lxc: LXC permission denied issue (LP: #1299588). + - core: mega-watcher for machines does not include container + addresses (LP: #1301464). + + -- James Page Mon, 07 Apr 2014 18:24:59 +0100 + juju-core (1.17.7-0ubuntu1) trusty; urgency=medium * New upstream point release, including fixes for: diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/client/client.go juju-core-1.18.0/src/github.com/joyent/gocommon/client/client.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/client/client.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/client/client.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,111 @@ +// +// gocommon - Go library to interact with the JoyentCloud +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package client + +import ( + "fmt" + "net/url" + "strings" + "sync" + "time" + + "github.com/juju/loggo" + + joyenthttp "github.com/joyent/gocommon/http" + "github.com/joyent/gosign/auth" +) + +const ( + // The HTTP request methods. + GET = "GET" + POST = "POST" + PUT = "PUT" + DELETE = "DELETE" + HEAD = "HEAD" + COPY = "COPY" +) + +// Client implementations sends service requests to the JoyentCloud. +type Client interface { + SendRequest(method, apiCall, rfc1123Date string, request *joyenthttp.RequestData, response *joyenthttp.ResponseData) (err error) + // MakeServiceURL prepares a full URL to a service endpoint, with optional + // URL parts. It uses the first endpoint it can find for the given service type. + MakeServiceURL(parts []string) string + SignURL(path string, expires time.Time) (string, error) +} + +// This client sends requests without authenticating. +type client struct { + mu sync.Mutex + logger *loggo.Logger + baseURL string + creds *auth.Credentials + httpClient *joyenthttp.Client +} + +var _ Client = (*client)(nil) + +func newClient(baseURL string, credentials *auth.Credentials, httpClient *joyenthttp.Client, logger *loggo.Logger) Client { + client := client{baseURL: baseURL, logger: logger, creds: credentials, httpClient: httpClient} + return &client +} + +func NewClient(baseURL, apiVersion string, credentials *auth.Credentials, logger *loggo.Logger) Client { + sharedHttpClient := joyenthttp.New(credentials, apiVersion, logger) + return newClient(baseURL, credentials, sharedHttpClient, logger) +} + +func (c *client) sendRequest(method, url, rfc1123Date string, request *joyenthttp.RequestData, response *joyenthttp.ResponseData) (err error) { + if request.ReqValue != nil || response.RespValue != nil { + err = c.httpClient.JsonRequest(method, url, rfc1123Date, request, response) + } else { + err = c.httpClient.BinaryRequest(method, url, rfc1123Date, request, response) + } + return +} + +func (c *client) SendRequest(method, apiCall, rfc1123Date string, request *joyenthttp.RequestData, response *joyenthttp.ResponseData) (err error) { + url := c.MakeServiceURL([]string{c.creds.UserAuthentication.User, apiCall}) + err = c.sendRequest(method, url, rfc1123Date, request, response) + return +} + +func makeURL(base string, parts []string) string { + if !strings.HasSuffix(base, "/") && len(parts) > 0 { + base += "/" + } + if parts[1] == "" { + return base + parts[0] + } + return base + strings.Join(parts, "/") +} + +func (c *client) MakeServiceURL(parts []string) string { + return makeURL(c.baseURL, parts) +} + +func (c *client) SignURL(path string, expires time.Time) (string, error) { + parsedURL, err := url.Parse(c.baseURL) + if err != nil { + return "", fmt.Errorf("bad Manta endpoint URL %q: %v", c.baseURL, err) + } + userAuthentication := c.creds.UserAuthentication + userAuthentication.Algorithm = "RSA-SHA1" + keyId := url.QueryEscape(fmt.Sprintf("/%s/keys/%s", userAuthentication.User, c.creds.MantaKeyId)) + params := fmt.Sprintf("algorithm=%s&expires=%d&keyId=%s", userAuthentication.Algorithm, expires.Unix(), keyId) + signingLine := fmt.Sprintf("GET\n%s\n%s\n%s", parsedURL.Host, path, params) + + signature, err := auth.GetSignature(&userAuthentication, signingLine) + if err != nil { + return "", fmt.Errorf("cannot generate URL signature: %v", err) + } + signedURL := fmt.Sprintf("%s%s?%s&signature=%s", c.baseURL, path, params, url.QueryEscape(signature)) + return signedURL, nil +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/client/client_test.go juju-core-1.18.0/src/github.com/joyent/gocommon/client/client_test.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/client/client_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/client/client_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,64 @@ +// +// gocommon - Go library to interact with the JoyentCloud +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package client_test + +import ( + "flag" + "fmt" + "github.com/joyent/gocommon/client" + joyenthttp "github.com/joyent/gocommon/http" + "github.com/joyent/gocommon/jpc" + "github.com/joyent/gosign/auth" + gc "launchpad.net/gocheck" + "net/http" + "testing" + "time" +) + +type ClientSuite struct { + creds *auth.Credentials +} + +var keyName = flag.String("key.name", "", "Specify the full path to the private key, defaults to ~/.ssh/id_rsa") + +func Test(t *testing.T) { + creds, err := jpc.CompleteCredentialsFromEnv(*keyName) + if err != nil { + t.Fatalf("Error setting up test suite: %v", err) + } + + gc.Suite(&ClientSuite{creds: creds}) + gc.TestingT(t) +} + +func (s *ClientSuite) TestNewClient(c *gc.C) { + cl := client.NewClient(s.creds.SdcEndpoint.URL, "", s.creds, nil) + c.Assert(cl, gc.NotNil) +} + +func (s *ClientSuite) TestSendRequest(c *gc.C) { + cl := client.NewClient(s.creds.SdcEndpoint.URL, "", s.creds, nil) + c.Assert(cl, gc.NotNil) + + req := joyenthttp.RequestData{} + resp := joyenthttp.ResponseData{ExpectedStatus: []int{http.StatusOK}} + err := cl.SendRequest(client.GET, "", "", &req, &resp) + c.Assert(err, gc.IsNil) +} + +func (s *ClientSuite) TestSignURL(c *gc.C) { + cl := client.NewClient(s.creds.MantaEndpoint.URL, "", s.creds, nil) + c.Assert(cl, gc.NotNil) + + path := fmt.Sprintf("/%s/stor", s.creds.UserAuthentication.User) + singedUrl, err := cl.SignURL(path, time.Now().Add(time.Minute*5)) + c.Assert(err, gc.IsNil) + c.Assert(singedUrl, gc.Not(gc.Equals), "") +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/COPYING juju-core-1.18.0/src/github.com/joyent/gocommon/COPYING --- juju-core-1.17.7/src/github.com/joyent/gocommon/COPYING 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/COPYING 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/COPYING.LESSER juju-core-1.18.0/src/github.com/joyent/gocommon/COPYING.LESSER --- juju-core-1.17.7/src/github.com/joyent/gocommon/COPYING.LESSER 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/COPYING.LESSER 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/errors/errors.go juju-core-1.18.0/src/github.com/joyent/gocommon/errors/errors.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/errors/errors.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/errors/errors.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,292 @@ +// +// gocommon - Go library to interact with the JoyentCloud +// This package provides an Error implementation which knows about types of error, and which has support +// for error causes. +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package errors + +import "fmt" + +type Code string + +const ( + // Public available error types. + // These errors are provided because they are specifically required by business logic in the callers. + BadRequestError = Code("BadRequest") + InternalErrorError = Code("InternalError") + InvalidArgumentError = Code("InvalidArgument") + InvalidCredentialsError = Code("InvalidCredentials") + InvalidHeaderError = Code("InvalidHeader") + InvalidVersionError = Code("InvalidVersion") + MissingParameterError = Code("MissinParameter") + NotAuthorizedError = Code("NotAuthorized") + RequestThrottledError = Code("RequestThrottled") + RequestTooLargeError = Code("RequestTooLarge") + RequestMovedError = Code("RequestMoved") + ResourceNotFoundError = Code("ResourceNotFound") + UnknownErrorError = Code("UnkownError") +) + +// Error instances store an optional error cause. +type Error interface { + error + Cause() error +} + +type gojoyentError struct { + error + errcode Code + cause error +} + +// Type checks. +var _ Error = (*gojoyentError)(nil) + +// Code returns the error code. +func (err *gojoyentError) code() Code { + if err.errcode != UnknownErrorError { + return err.errcode + } + if e, ok := err.cause.(*gojoyentError); ok { + return e.code() + } + return UnknownErrorError +} + +// Cause returns the error cause. +func (err *gojoyentError) Cause() error { + return err.cause +} + +// CausedBy returns true if this error or its cause are of the specified error code. +func (err *gojoyentError) causedBy(code Code) bool { + if err.code() == code { + return true + } + if cause, ok := err.cause.(*gojoyentError); ok { + return cause.code() == code + } + return false +} + +// Error fulfills the error interface, taking account of any caused by error. +func (err *gojoyentError) Error() string { + if err.cause != nil { + return fmt.Sprintf("%v\ncaused by: %v", err.error, err.cause) + } + return err.error.Error() +} + +func IsBadRequest(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(BadRequestError) + } + return false +} + +func IsInternalError(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(InternalErrorError) + } + return false +} + +func IsInvalidArgument(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(InvalidArgumentError) + } + return false +} + +func IsInvalidCredentials(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(InvalidCredentialsError) + } + return false +} + +func IsInvalidHeader(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(InvalidHeaderError) + } + return false +} + +func IsInvalidVersion(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(InvalidVersionError) + } + return false +} + +func IsMissingParameter(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(MissingParameterError) + } + return false +} + +func IsNotAuthorized(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(NotAuthorizedError) + } + return false +} + +func IsRequestThrottled(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(RequestThrottledError) + } + return false +} + +func IsRequestTooLarge(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(RequestTooLargeError) + } + return false +} + +func IsRequestMoved(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(RequestMovedError) + } + return false +} + +func IsResourceNotFound(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(ResourceNotFoundError) + } + return false +} + +func IsUnknownError(err error) bool { + if e, ok := err.(*gojoyentError); ok { + return e.causedBy(UnknownErrorError) + } + return false +} + +// New creates a new Error instance with the specified cause. +func makeErrorf(code Code, cause error, format string, args ...interface{}) Error { + return &gojoyentError{ + errcode: code, + error: fmt.Errorf(format, args...), + cause: cause, + } +} + +// New creates a new UnknownError Error instance with the specified cause. +func Newf(cause error, format string, args ...interface{}) Error { + return makeErrorf(UnknownErrorError, cause, format, args...) +} + +// New creates a new BadRequest Error instance with the specified cause. +func NewBadRequestf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Bad Request: %s", context) + } + return makeErrorf(BadRequestError, cause, format, args...) +} + +// New creates a new InternalError Error instance with the specified cause. +func NewInternalErrorf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Internal Error: %s", context) + } + return makeErrorf(InternalErrorError, cause, format, args...) +} + +// New creates a new InvalidArgument Error instance with the specified cause. +func NewInvalidArgumentf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Invalid Argument: %s", context) + } + return makeErrorf(InvalidArgumentError, cause, format, args...) +} + +// New creates a new InvalidCredentials Error instance with the specified cause. +func NewInvalidCredentialsf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Invalid Credentials: %s", context) + } + return makeErrorf(InvalidCredentialsError, cause, format, args...) +} + +// New creates a new InvalidHeader Error instance with the specified cause. +func NewInvalidHeaderf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Invalid Header: %s", context) + } + return makeErrorf(InvalidHeaderError, cause, format, args...) +} + +// New creates a new InvalidVersion Error instance with the specified cause. +func NewInvalidVersionf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Invalid Version: %s", context) + } + return makeErrorf(InvalidVersionError, cause, format, args...) +} + +// New creates a new MissingParameter Error instance with the specified cause. +func NewMissingParameterf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Missing Parameter: %s", context) + } + return makeErrorf(MissingParameterError, cause, format, args...) +} + +// New creates a new NotAuthorized Error instance with the specified cause. +func NewNotAuthorizedf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Not Authorized: %s", context) + } + return makeErrorf(NotAuthorizedError, cause, format, args...) +} + +// New creates a new RequestThrottled Error instance with the specified cause. +func NewRequestThrottledf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Request Throttled: %s", context) + } + return makeErrorf(RequestThrottledError, cause, format, args...) +} + +// New creates a new RequestTooLarge Error instance with the specified cause. +func NewRequestTooLargef(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Request Too Large: %s", context) + } + return makeErrorf(RequestTooLargeError, cause, format, args...) +} + +// New creates a new RequestMoved Error instance with the specified cause. +func NewRequestMovedf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Request Moved: %s", context) + } + return makeErrorf(RequestMovedError, cause, format, args...) +} + +// New creates a new ResourceNotFound Error instance with the specified cause. +func NewResourceNotFoundf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Resource Not Found: %s", context) + } + return makeErrorf(ResourceNotFoundError, cause, format, args...) +} + +// New creates a new UnknownError Error instance with the specified cause. +func NewUnknownErrorf(cause error, context interface{}, format string, args ...interface{}) Error { + if format == "" { + format = fmt.Sprintf("Unknown Error: %s", context) + } + return makeErrorf(UnknownErrorError, cause, format, args...) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/errors/errors_test.go juju-core-1.18.0/src/github.com/joyent/gocommon/errors/errors_test.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/errors/errors_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/errors/errors_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,224 @@ +// +// gocommon - Go library to interact with the JoyentCloud +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package errors_test + +import ( + "github.com/joyent/gocommon/errors" + gc "launchpad.net/gocheck" + "testing" +) + +func Test(t *testing.T) { gc.TestingT(t) } + +type ErrorsSuite struct { +} + +var _ = gc.Suite(&ErrorsSuite{}) + +func (s *ErrorsSuite) TestCreateSimpleBadRequestError(c *gc.C) { + context := "context" + err := errors.NewBadRequestf(nil, context, "") + c.Assert(errors.IsBadRequest(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Bad Request: context") +} + +func (s *ErrorsSuite) TestCreateBadRequestError(c *gc.C) { + context := "context" + err := errors.NewBadRequestf(nil, context, "It was bad request: %s", context) + c.Assert(errors.IsBadRequest(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was bad request: context") +} + +func (s *ErrorsSuite) TestCreateSimpleInternalErrorError(c *gc.C) { + context := "context" + err := errors.NewInternalErrorf(nil, context, "") + c.Assert(errors.IsInternalError(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Internal Error: context") +} + +func (s *ErrorsSuite) TestCreateInternalErrorError(c *gc.C) { + context := "context" + err := errors.NewInternalErrorf(nil, context, "It was internal error: %s", context) + c.Assert(errors.IsInternalError(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was internal error: context") +} + +func (s *ErrorsSuite) TestCreateSimpleInvalidArgumentError(c *gc.C) { + context := "context" + err := errors.NewInvalidArgumentf(nil, context, "") + c.Assert(errors.IsInvalidArgument(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Invalid Argument: context") +} + +func (s *ErrorsSuite) TestCreateInvalidArgumentError(c *gc.C) { + context := "context" + err := errors.NewInvalidArgumentf(nil, context, "It was invalid argument: %s", context) + c.Assert(errors.IsInvalidArgument(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was invalid argument: context") +} + +func (s *ErrorsSuite) TestCreateSimpleInvalidCredentialsError(c *gc.C) { + context := "context" + err := errors.NewInvalidCredentialsf(nil, context, "") + c.Assert(errors.IsInvalidCredentials(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Invalid Credentials: context") +} + +func (s *ErrorsSuite) TestCreateInvalidCredentialsError(c *gc.C) { + context := "context" + err := errors.NewInvalidCredentialsf(nil, context, "It was invalid credentials: %s", context) + c.Assert(errors.IsInvalidCredentials(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was invalid credentials: context") +} + +func (s *ErrorsSuite) TestCreateSimpleInvalidHeaderError(c *gc.C) { + context := "context" + err := errors.NewInvalidHeaderf(nil, context, "") + c.Assert(errors.IsInvalidHeader(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Invalid Header: context") +} + +func (s *ErrorsSuite) TestCreateInvalidHeaderError(c *gc.C) { + context := "context" + err := errors.NewInvalidHeaderf(nil, context, "It was invalid header: %s", context) + c.Assert(errors.IsInvalidHeader(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was invalid header: context") +} + +func (s *ErrorsSuite) TestCreateSimpleInvalidVersionError(c *gc.C) { + context := "context" + err := errors.NewInvalidVersionf(nil, context, "") + c.Assert(errors.IsInvalidVersion(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Invalid Version: context") +} + +func (s *ErrorsSuite) TestCreateInvalidVersionError(c *gc.C) { + context := "context" + err := errors.NewInvalidVersionf(nil, context, "It was invalid version: %s", context) + c.Assert(errors.IsInvalidVersion(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was invalid version: context") +} + +func (s *ErrorsSuite) TestCreateSimpleMissingParameterError(c *gc.C) { + context := "context" + err := errors.NewMissingParameterf(nil, context, "") + c.Assert(errors.IsMissingParameter(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Missing Parameter: context") +} + +func (s *ErrorsSuite) TestCreateMissingParameterError(c *gc.C) { + context := "context" + err := errors.NewMissingParameterf(nil, context, "It was missing parameter: %s", context) + c.Assert(errors.IsMissingParameter(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was missing parameter: context") +} + +func (s *ErrorsSuite) TestCreateSimpleNotAuthorizedError(c *gc.C) { + context := "context" + err := errors.NewNotAuthorizedf(nil, context, "") + c.Assert(errors.IsNotAuthorized(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Not Authorized: context") +} + +func (s *ErrorsSuite) TestCreateNotAuthorizedError(c *gc.C) { + context := "context" + err := errors.NewNotAuthorizedf(nil, context, "It was not authorized: %s", context) + c.Assert(errors.IsNotAuthorized(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was not authorized: context") +} + +func (s *ErrorsSuite) TestCreateSimpleRequestThrottledError(c *gc.C) { + context := "context" + err := errors.NewRequestThrottledf(nil, context, "") + c.Assert(errors.IsRequestThrottled(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Request Throttled: context") +} + +func (s *ErrorsSuite) TestCreateRequestThrottledError(c *gc.C) { + context := "context" + err := errors.NewRequestThrottledf(nil, context, "It was request throttled: %s", context) + c.Assert(errors.IsRequestThrottled(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was request throttled: context") +} + +func (s *ErrorsSuite) TestCreateSimpleRequestTooLargeError(c *gc.C) { + context := "context" + err := errors.NewRequestTooLargef(nil, context, "") + c.Assert(errors.IsRequestTooLarge(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Request Too Large: context") +} + +func (s *ErrorsSuite) TestCreateRequestTooLargeError(c *gc.C) { + context := "context" + err := errors.NewRequestTooLargef(nil, context, "It was request too large: %s", context) + c.Assert(errors.IsRequestTooLarge(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was request too large: context") +} + +func (s *ErrorsSuite) TestCreateSimpleRequestMovedError(c *gc.C) { + context := "context" + err := errors.NewRequestMovedf(nil, context, "") + c.Assert(errors.IsRequestMoved(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Request Moved: context") +} + +func (s *ErrorsSuite) TestCreateRequestMovedError(c *gc.C) { + context := "context" + err := errors.NewRequestMovedf(nil, context, "It was request moved: %s", context) + c.Assert(errors.IsRequestMoved(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was request moved: context") +} + +func (s *ErrorsSuite) TestCreateSimpleResourceNotFoundError(c *gc.C) { + context := "context" + err := errors.NewResourceNotFoundf(nil, context, "") + c.Assert(errors.IsResourceNotFound(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Resource Not Found: context") +} + +func (s *ErrorsSuite) TestCreateResourceNotFoundError(c *gc.C) { + context := "context" + err := errors.NewResourceNotFoundf(nil, context, "It was resource not found: %s", context) + c.Assert(errors.IsResourceNotFound(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was resource not found: context") +} + +func (s *ErrorsSuite) TestCreateSimpleUnknownErrorError(c *gc.C) { + context := "context" + err := errors.NewUnknownErrorf(nil, context, "") + c.Assert(errors.IsUnknownError(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "Unknown Error: context") +} + +func (s *ErrorsSuite) TestCreateUnknownErrorError(c *gc.C) { + context := "context" + err := errors.NewUnknownErrorf(nil, context, "It was unknown error: %s", context) + c.Assert(errors.IsUnknownError(err), gc.Equals, true) + c.Assert(err.Error(), gc.Equals, "It was unknown error: context") +} + +func (s *ErrorsSuite) TestErrorCause(c *gc.C) { + rootCause := errors.NewResourceNotFoundf(nil, "some value", "") + // Construct a new error, based on a resource not found root cause. + err := errors.Newf(rootCause, "an error occurred") + c.Assert(err.Cause(), gc.Equals, rootCause) + // Check the other error attributes. + c.Assert(err.Error(), gc.Equals, "an error occurred\ncaused by: Resource Not Found: some value") +} + +func (s *ErrorsSuite) TestErrorIsType(c *gc.C) { + rootCause := errors.NewBadRequestf(nil, "some value", "") + // Construct a new error, based on a bad request root cause. + err := errors.Newf(rootCause, "an error occurred") + // Check that the error is not falsely identified as something it is not. + c.Assert(errors.IsNotAuthorized(err), gc.Equals, false) + // Check that the error is correctly identified as a not found error. + c.Assert(errors.IsBadRequest(err), gc.Equals, true) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/.gitignore juju-core-1.18.0/src/github.com/joyent/gocommon/.gitignore --- juju-core-1.17.7/src/github.com/joyent/gocommon/.gitignore 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/.gitignore 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,26 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe + +# IntelliJ files +.idea +*.iml \ No newline at end of file diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/gocommon.go juju-core-1.18.0/src/github.com/joyent/gocommon/gocommon.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/gocommon.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/gocommon.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,18 @@ +/* +The gocommon package collects common packages to interact with the Joyent Public Cloud and Joyent Manta services. + +The gocommon package is structured as follow: + + - gocommon/client. Client for sending requests. + - gocommon/errors. Joyent specific errors. + - gocommon/http. HTTP client for sending requests. + - gocommon/jpc. This package provides common structures and functions across packages. + - gocommon/testing. Testing Suite for local testing. + +Licensed under LGPL v3. + +Copyright (c) 2013 Joyent Inc. +Written by Daniele Stroppa + +*/ +package gocommon diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/gocommon_test.go juju-core-1.18.0/src/github.com/joyent/gocommon/gocommon_test.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/gocommon_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/gocommon_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,24 @@ +// +// gocommon - Go library to interact with the JoyentCloud +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package gocommon + +import ( + gc "launchpad.net/gocheck" + "testing" +) + +func Test(t *testing.T) { + gc.TestingT(t) +} + +type GoCommonTestSuite struct { +} + +var _ = gc.Suite(&GoCommonTestSuite{}) diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/http/client.go juju-core-1.18.0/src/github.com/joyent/gocommon/http/client.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/http/client.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/http/client.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,420 @@ +// +// gocommon - Go library to interact with the JoyentCloud +// An HTTP Client which sends json and binary requests, handling data marshalling and response processing. +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package http + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "io/ioutil" + "net/http" + "net/url" + "reflect" + "strconv" + "strings" + "time" + + "github.com/juju/loggo" + + "github.com/joyent/gocommon" + "github.com/joyent/gocommon/errors" + "github.com/joyent/gocommon/jpc" + "github.com/joyent/gosign/auth" +) + +const ( + contentTypeJSON = "application/json" + contentTypeOctetStream = "application/octet-stream" +) + +type Client struct { + http.Client + maxSendAttempts int + credentials *auth.Credentials + apiVersion string + logger *loggo.Logger +} + +type ErrorResponse struct { + Message string `json:"message"` + Code int `json:"code"` +} + +func (e *ErrorResponse) Error() string { + return fmt.Sprintf("Failed: %d: %s", e.Code, e.Message) +} + +type ErrorWrapper struct { + Error ErrorResponse `json:"error"` +} + +type RequestData struct { + ReqHeaders http.Header + Params *url.Values + ReqValue interface{} + ReqReader io.Reader + ReqLength int +} + +type ResponseData struct { + ExpectedStatus []int + RespHeaders *http.Header + RespValue interface{} + RespReader io.ReadCloser +} + +const ( + // The maximum number of times to try sending a request before we give up + // (assuming any unsuccessful attempts can be sensibly tried again). + MaxSendAttempts = 3 +) + +// New returns a new http *Client using the default net/http client. +func New(credentials *auth.Credentials, apiVersion string, logger *loggo.Logger) *Client { + return &Client{*http.DefaultClient, MaxSendAttempts, credentials, apiVersion, logger} +} + +func gojoyentAgent() string { + return fmt.Sprintf("gocommon (%s)", gocommon.Version) +} + +func createHeaders(extraHeaders http.Header, credentials *auth.Credentials, contentType, rfc1123Date, + apiVersion string, isMantaRequest bool) (http.Header, error) { + + headers := make(http.Header) + if extraHeaders != nil { + for header, values := range extraHeaders { + for _, value := range values { + headers.Add(header, value) + } + } + } + if extraHeaders.Get("Content-Type") == "" { + headers.Add("Content-Type", contentType) + } + if extraHeaders.Get("Accept") == "" { + headers.Add("Accept", contentType) + } + if rfc1123Date != "" { + headers.Set("Date", rfc1123Date) + } else { + headers.Set("Date", getDateForRegion(credentials, isMantaRequest)) + } + authHeaders, err := auth.CreateAuthorizationHeader(headers, credentials, isMantaRequest) + if err != nil { + return http.Header{}, err + } + headers.Set("Authorization", authHeaders) + if apiVersion != "" { + headers.Set("X-Api-Version", apiVersion) + } + headers.Add("User-Agent", gojoyentAgent()) + return headers, nil +} + +func getDateForRegion(credentials *auth.Credentials, isManta bool) string { + if isManta { + location, _ := time.LoadLocation(jpc.Locations["us-east-1"]) + return time.Now().In(location).Format(time.RFC1123) + } else { + location, _ := time.LoadLocation(jpc.Locations[credentials.Region()]) + return time.Now().In(location).Format(time.RFC1123) + } +} + +// JsonRequest JSON encodes and sends the object in reqData.ReqValue (if any) to the specified URL. +// Optional method arguments are passed using the RequestData object. +// Relevant RequestData fields: +// ReqHeaders: additional HTTP header values to add to the request. +// ExpectedStatus: the allowed HTTP response status values, else an error is returned. +// ReqValue: the data object to send. +// RespValue: the data object to decode the result into. +func (c *Client) JsonRequest(method, url, rfc1123Date string, request *RequestData, response *ResponseData) (err error) { + err = nil + var body []byte + if request.Params != nil { + url += "?" + request.Params.Encode() + } + if request.ReqValue != nil { + body, err = json.Marshal(request.ReqValue) + if err != nil { + err = errors.Newf(err, "failed marshalling the request body") + return + } + } + headers, err := createHeaders(request.ReqHeaders, c.credentials, contentTypeJSON, rfc1123Date, c.apiVersion, + isMantaRequest(url, c.credentials.UserAuthentication.User)) + if err != nil { + return err + } + respBody, respHeader, err := c.sendRequest( + method, url, bytes.NewReader(body), len(body), headers, response.ExpectedStatus, c.logger) + if err != nil { + return + } + defer respBody.Close() + respData, err := ioutil.ReadAll(respBody) + if err != nil { + err = errors.Newf(err, "failed reading the response body") + return + } + + if len(respData) > 0 { + if response.RespValue != nil { + if _, ok := response.RespValue.(*[]byte); ok { + response.RespValue = respData + //err = decodeJSON(bytes.NewReader(respData), false, response.RespValue) + //if err != nil { + // err = errors.Newf(err, "failed unmarshaling/decoding the response body: %s", respData) + //} + } else { + err = json.Unmarshal(respData, response.RespValue) + if err != nil { + err = decodeJSON(bytes.NewReader(respData), true, response.RespValue) + if err != nil { + err = errors.Newf(err, "failed unmarshaling/decoding the response body: %s", respData) + } + } + } + } + } + + if respHeader != nil { + response.RespHeaders = respHeader + } + + return +} + +func decodeJSON(r io.Reader, multiple bool, into interface{}) error { + d := json.NewDecoder(r) + if multiple { + return decodeStream(d, into) + } + return d.Decode(into) +} + +func decodeStream(d *json.Decoder, into interface{}) error { + t := reflect.TypeOf(into) + if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Slice { + return fmt.Errorf("unexpected type %s", t) + } + elemType := t.Elem().Elem() + slice := reflect.ValueOf(into).Elem() + for { + val := reflect.New(elemType) + if err := d.Decode(val.Interface()); err != nil { + if err == io.EOF { + break + } + return err + } + slice.Set(reflect.Append(slice, val.Elem())) + } + return nil +} + +// Sends the byte array in reqData.ReqValue (if any) to the specified URL. +// Optional method arguments are passed using the RequestData object. +// Relevant RequestData fields: +// ReqHeaders: additional HTTP header values to add to the request. +// ExpectedStatus: the allowed HTTP response status values, else an error is returned. +// ReqReader: an io.Reader providing the bytes to send. +// RespReader: assigned an io.ReadCloser instance used to read the returned data.. +func (c *Client) BinaryRequest(method, url, rfc1123Date string, request *RequestData, response *ResponseData) (err error) { + err = nil + + if request.Params != nil { + url += "?" + request.Params.Encode() + } + headers, err := createHeaders(request.ReqHeaders, c.credentials, contentTypeOctetStream, rfc1123Date, + c.apiVersion, isMantaRequest(url, c.credentials.UserAuthentication.User)) + if err != nil { + return err + } + respBody, respHeader, err := c.sendRequest( + method, url, request.ReqReader, request.ReqLength, headers, response.ExpectedStatus, c.logger) + if err != nil { + return + } + if response.RespReader != nil { + response.RespReader = respBody + } + if respHeader != nil { + response.RespHeaders = respHeader + } + return +} + +// Sends the specified request to URL and checks that the HTTP response status is as expected. +// reqReader: a reader returning the data to send. +// length: the number of bytes to send. +// headers: HTTP headers to include with the request. +// expectedStatus: a slice of allowed response status codes. +func (c *Client) sendRequest(method, URL string, reqReader io.Reader, length int, headers http.Header, + expectedStatus []int, logger *loggo.Logger) (rc io.ReadCloser, respHeader *http.Header, err error) { + reqData := make([]byte, length) + if reqReader != nil { + nrRead, err := io.ReadFull(reqReader, reqData) + if err != nil { + err = errors.Newf(err, "failed reading the request data, read %v of %v bytes", nrRead, length) + return rc, respHeader, err + } + } + rawResp, err := c.sendRateLimitedRequest(method, URL, headers, reqData, logger) + if err != nil { + return + } + + if logger != nil && logger.IsTraceEnabled() { + logger.Tracef("Request: %s %s\n", method, URL) + logger.Tracef("Request header: %s\n", headers) + logger.Tracef("Request body: %s\n", reqData) + logger.Tracef("Response: %s\n", rawResp.Status) + logger.Tracef("Response header: %s\n", rawResp.Header) + logger.Tracef("Response body: %s\n", rawResp.Body) + logger.Tracef("Response error: %s\n", err) + } + + foundStatus := false + if len(expectedStatus) == 0 { + expectedStatus = []int{http.StatusOK} + } + for _, status := range expectedStatus { + if rawResp.StatusCode == status { + foundStatus = true + break + } + } + if !foundStatus && len(expectedStatus) > 0 { + err = handleError(URL, rawResp) + rawResp.Body.Close() + return + } + return rawResp.Body, &rawResp.Header, err +} + +func (c *Client) sendRateLimitedRequest(method, URL string, headers http.Header, reqData []byte, + logger *loggo.Logger) (resp *http.Response, err error) { + for i := 0; i < c.maxSendAttempts; i++ { + var reqReader io.Reader + if reqData != nil { + reqReader = bytes.NewReader(reqData) + } + req, err := http.NewRequest(method, URL, reqReader) + if err != nil { + err = errors.Newf(err, "failed creating the request %s", URL) + return nil, err + } + // Setting req.Close to true to avoid malformed HTTP version "nullHTTP/1.1" error + // See http://stackoverflow.com/questions/17714494/golang-http-request-results-in-eof-errors-when-making-multiple-requests-successi + req.Close = true + for header, values := range headers { + for _, value := range values { + req.Header.Add(header, value) + } + } + req.ContentLength = int64(len(reqData)) + resp, err = c.Do(req) + if err != nil { + return nil, errors.Newf(err, "failed executing the request %s", URL) + } + if resp.StatusCode != http.StatusRequestEntityTooLarge || resp.Header.Get("Retry-After") == "" { + return resp, nil + } + resp.Body.Close() + retryAfter, err := strconv.ParseFloat(resp.Header.Get("Retry-After"), 64) + if err != nil { + return nil, errors.Newf(err, "Invalid Retry-After header %s", URL) + } + if retryAfter == 0 { + return nil, errors.Newf(err, "Resource limit exeeded at URL %s", URL) + } + if logger != nil { + logger.Warningf("Too many requests, retrying in %dms.", int(retryAfter*1000)) + } + time.Sleep(time.Duration(retryAfter) * time.Second) + } + return nil, errors.Newf(err, "Maximum number of attempts (%d) reached sending request to %s", c.maxSendAttempts, URL) +} + +type HttpError struct { + StatusCode int + Data map[string][]string + Url string + ResponseMessage string +} + +func (e *HttpError) Error() string { + return fmt.Sprintf("request %q returned unexpected status %d with body %q", + e.Url, + e.StatusCode, + e.ResponseMessage, + ) +} + +// The HTTP response status code was not one of those expected, so we construct an error. +// NotFound (404) codes have their own NotFound error type. +// We also make a guess at duplicate value errors. +func handleError(URL string, resp *http.Response) error { + errBytes, _ := ioutil.ReadAll(resp.Body) + errInfo := string(errBytes) + // Check if we have a JSON representation of the failure, if so decode it. + if resp.Header.Get("Content-Type") == contentTypeJSON { + var errResponse ErrorResponse + if err := json.Unmarshal(errBytes, &errResponse); err == nil { + errInfo = errResponse.Message + } + } + httpError := &HttpError{ + resp.StatusCode, map[string][]string(resp.Header), URL, errInfo, + } + switch resp.StatusCode { + case http.StatusBadRequest: + return errors.NewBadRequestf(httpError, "", "Bad request %s", URL) + case http.StatusUnauthorized: + return errors.NewNotAuthorizedf(httpError, "", "Unauthorised URL %s", URL) + //return errors.NewInvalidCredentialsf(httpError, "", "Unauthorised URL %s", URL) + case http.StatusForbidden: + //return errors. + case http.StatusNotFound: + return errors.NewResourceNotFoundf(httpError, "", "Resource not found %s", URL) + case http.StatusMethodNotAllowed: + //return errors. + case http.StatusNotAcceptable: + return errors.NewInvalidHeaderf(httpError, "", "Invalid Header %s", URL) + case http.StatusConflict: + return errors.NewMissingParameterf(httpError, "", "Missing parameters %s", URL) + //return errors.NewInvalidArgumentf(httpError, "", "Invalid parameter %s", URL) + case http.StatusRequestEntityTooLarge: + return errors.NewRequestTooLargef(httpError, "", "Request too large %s", URL) + case http.StatusUnsupportedMediaType: + //return errors. + case http.StatusServiceUnavailable: + return errors.NewInternalErrorf(httpError, "", "Internal error %s", URL) + case 420: + // SlowDown + return errors.NewRequestThrottledf(httpError, "", "Request throttled %s", URL) + case 422: + // Unprocessable Entity + return errors.NewInvalidArgumentf(httpError, "", "Invalid parameters %s", URL) + case 449: + // RetryWith + return errors.NewInvalidVersionf(httpError, "", "Invalid version %s", URL) + //RequestMovedError -> ? + } + + return errors.NewUnknownErrorf(httpError, "", "Unknown error %s", URL) +} + +func isMantaRequest(url, user string) bool { + return strings.Contains(url, "/"+user+"/stor") || strings.Contains(url, "/"+user+"/jobs") || strings.Contains(url, "/"+user+"/public") +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/http/client_test.go juju-core-1.18.0/src/github.com/joyent/gocommon/http/client_test.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/http/client_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/http/client_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,201 @@ +package http + +import ( + "bytes" + "fmt" + "io/ioutil" + gc "launchpad.net/gocheck" + "net/http" + "testing" + + httpsuite "github.com/joyent/gocommon/testing" + "github.com/joyent/gosign/auth" +) + +const ( + Signature = "yK0J17CQ04ZvMsFLoH163Sjyg8tE4BoIeCsmKWLQKN3BYgSpR0XyqrecheQ2A0o4L99oSumYSKIscBSiH5rqdf4/1zC/FEkYOI2UzcIHYb1MPNzO3g/5X44TppYE+8dxoH99V+Ts8RT3ZurEYjQ8wmK0TnxdirAevSpbypZJaBOFXUZSxx80m5BD4QE/MSGo/eaVdJI/Iw+nardHNvimVCr6pRNycX1I4FdyRR6kgrAl2NkY2yxx/CAY21Ir+dmbG3A1x4GiIE485LLheAL5/toPo7Gh8G5fkrF9dXWVyX0k9AZXqXNWn5AZxc32dKL2enH09j/X86RtwiR1IEuPww==" + key = `-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEAyLOtVh8qXjdwfjZZYwkEgg1yoSzmpKKpmzYW745lBGtPH87F +spHVHeqjmgFnBsARsD7CHzYyQTho7oLrAEbuF7tKdGRK25wJIenPKKuL+UVwZNeJ +VEXSiMNmX3Y4IqRteqRIjhw3DmXYHEWvBc2JVy8lWtyK+o6o8jlO0aRTTT2+dETp +yqKqNJyHVNz2u6XVtm7jqyLU7tAqW+qpr5zSoNmuUAyz6JDCRnlWvwp1qzuS1LV3 +2HK9yfq8TGriDVPyPRpFRmiRGWGIrIKrmm4sImpoLfuVBITjeh8V3Ee0OCDmTLgY +lTHAmCLFJxaW5Y8b4cTt5pbT7R1iu77RKJo3fwIBIwKCAQEAmtPAOx9bMr0NozE9 +pCuHIn9nDp77EUpIUym57AAiCrkuaP6YgnB/1T/6jL9A2VJWycoDdynO/xzjO6bS +iy9nNuDwSyjMCH+vRgwjdyVAGBD+7rTmSFMewUZHqLpIj8CsOgm0UF7opLT3K8C6 +N60vbyRepS3KTEIqjvkCSfPLO5Sp38KZYXKg0/Abb21WDSEzWonjV8JfOyMfUYhh +7QCE+Nf8s3b+vxskOuCQq1WHoqo8CqXrMYVknkvnQuRFPuaOLEjMbJVqlTb9ns2V +SKxmo46R7fl2dKgMBll+Nec+3Dn2/Iq/qnHq34HF/rhz0uvQDv1w1cSEMjLQaHtH +yZMkgwKBgQDtIAY+yqcmGFYiHQT7V35QbmfeJX1/v9KgpcA7L9Qi6H2LgKPlZu3e +Fc5Pp8C82uIzxuKBbEqauoWAEfP7r2wn1EwoQGMdsY9MpPiScS5iwLKuiSWyyjyf +Snmq+wLwVMYr71ijCuD+Ydm2xGoYeogwkV+QuTOS79s7HGM5tAN9TQKBgQDYrXHR +Nc84Xt+86cWCXJ2rAhHoTMXQQSVXoc75CjBPM2oUH6iguVBM7dPG0ORU14+o7Q7Y +gUvQCV6xoWH05nESHG++sidRifM/HT07M1bSjbMPcFmeAeA0mTFodXfRN6dKyibb +5kHUHgkgsC8qpXZr1KsNR7BcvC+xKuG1qC1R+wKBgDz5mzS3xJTEbeuDzhS+uhSu +rP6b7RI4o+AqnyUpjlIehq7X76FjnEBsAdn377uIvdLMvebEEy8aBRJNwmVKXaPX +gUwt0FgXtyJWTowOeaRdb8Z7CbGht9EwaG3LhGmvZiiOANl303Sc0ZVltOG5G7S3 +qtwSXbgRyqjMyQ7WhI3vAoGBAMYa67f2rtRz/8Kp2Sa7E8+M3Swo719RgTotighD +1GWrWav/sB3rQhpzCsRnNyj/mUn9T2bcnRX58C1gWY9zmpQ3QZhoXnZvf0+ltFNi +I36tcIMk5DixQgQ0Sm4iQalXdGGi4bMbqeaB3HWoZaNVc5XJwPYy6mNqOjuU657F +pcdLAoGBAOQRc5kaZ3APKGHu64DzKh5TOam9J2gpRSD5kF2fIkAeaYWU0bE9PlqV +MUxNRzxbIC16eCKFUoDkErIXGQfIMUOm+aCT/qpoAdXIvuO7H0OYRjMDmbscSDEV +cQYaFsx8Z1KwMVBTwDtiGXhd+82+dKnXxH4bZC+WAKs7L79HqhER +-----END RSA PRIVATE KEY-----` +) + +func Test(t *testing.T) { + gc.TestingT(t) +} + +type LoopingHTTPSuite struct { + httpsuite.HTTPSuite + creds *auth.Credentials +} + +func (s *LoopingHTTPSuite) setupLoopbackRequest() (*http.Header, chan string, *Client) { + var headers http.Header + bodyChan := make(chan string, 1) + handler := func(resp http.ResponseWriter, req *http.Request) { + headers = req.Header + bodyBytes, _ := ioutil.ReadAll(req.Body) + req.Body.Close() + bodyChan <- string(bodyBytes) + resp.Header().Add("Content-Length", "0") + resp.WriteHeader(http.StatusNoContent) + resp.Write([]byte{}) + } + s.Mux.HandleFunc("/", handler) + client := New(s.creds, "", nil) + + return &headers, bodyChan, client +} + +type HTTPClientTestSuite struct { + LoopingHTTPSuite +} + +type HTTPSClientTestSuite struct { + LoopingHTTPSuite +} + +var _ = gc.Suite(&HTTPClientTestSuite{LoopingHTTPSuite{httpsuite.HTTPSuite{}, &auth.Credentials{ + UserAuthentication: auth.Auth{User: "test_user", PrivateKey: key, Algorithm: "rsa-sha256"}, + SdcKeyId: "test_key", + SdcEndpoint: auth.Endpoint{URL: "http://gotest.api.joyentcloud.com"}, +}}}) +var _ = gc.Suite(&HTTPSClientTestSuite{LoopingHTTPSuite{httpsuite.HTTPSuite{UseTLS: true}, &auth.Credentials{ + UserAuthentication: auth.Auth{User: "test_user", PrivateKey: key, Algorithm: "rsa-sha256"}, + SdcKeyId: "test_key", + SdcEndpoint: auth.Endpoint{URL: "http://gotest.api.joyentcloud.com"}, +}}}) + +func (s *HTTPClientTestSuite) assertHeaderValues(c *gc.C, apiVersion string) { + emptyHeaders := http.Header{} + date := "Mon, 14 Oct 2013 18:49:29 GMT" + headers, _ := createHeaders(emptyHeaders, s.creds, "content-type", date, apiVersion, false) + contentTypes := []string{"content-type"} + dateHeader := []string{"Mon, 14 Oct 2013 18:49:29 GMT"} + authorizationHeader := []string{"Signature keyId=\"/test_user/keys/test_key\",algorithm=\"rsa-sha256\" " + Signature} + headerData := map[string][]string{ + "Date": dateHeader, "Authorization": authorizationHeader, + "Content-Type": contentTypes, "Accept": contentTypes, "User-Agent": []string{gojoyentAgent()}} + if apiVersion != "" { + headerData["X-Api-Version"] = []string{apiVersion} + } + expectedHeaders := http.Header(headerData) + c.Assert(headers, gc.DeepEquals, expectedHeaders) + c.Assert(emptyHeaders, gc.DeepEquals, http.Header{}) +} + +func (s *HTTPClientTestSuite) TestCreateHeadersNoApiVersion(c *gc.C) { + s.assertHeaderValues(c, "") +} + +func (s *HTTPClientTestSuite) TestCreateHeadersWithApiVersion(c *gc.C) { + s.assertHeaderValues(c, "token") +} + +func (s *HTTPClientTestSuite) TestCreateHeadersCopiesSupplied(c *gc.C) { + initialHeaders := make(http.Header) + date := "Mon, 14 Oct 2013 18:49:29 GMT" + initialHeaders["Foo"] = []string{"Bar"} + contentType := contentTypeJSON + contentTypes := []string{contentType} + dateHeader := []string{"Mon, 14 Oct 2013 18:49:29 GMT"} + authorizationHeader := []string{"Signature keyId=\"/test_user/keys/test_key\",algorithm=\"rsa-sha256\" " + Signature} + headers, _ := createHeaders(initialHeaders, s.creds, contentType, date, "", false) + // it should not change the headers passed in + c.Assert(initialHeaders, gc.DeepEquals, http.Header{"Foo": []string{"Bar"}}) + // The initial headers should be in the output + c.Assert(headers, gc.DeepEquals, + http.Header{"Foo": []string{"Bar"}, "Date": dateHeader, "Authorization": authorizationHeader, + "Content-Type": contentTypes, "Accept": contentTypes, "User-Agent": []string{gojoyentAgent()}}) +} + +func (s *HTTPClientTestSuite) TestBinaryRequestSetsUserAgent(c *gc.C) { + headers, _, client := s.setupLoopbackRequest() + req := RequestData{} + resp := ResponseData{ExpectedStatus: []int{http.StatusNoContent}} + err := client.BinaryRequest("POST", s.Server.URL, "", &req, &resp) + c.Assert(err, gc.IsNil) + agent := headers.Get("User-Agent") + c.Check(agent, gc.Not(gc.Equals), "") + c.Check(agent, gc.Equals, gojoyentAgent()) +} + +func (s *HTTPClientTestSuite) TestJSONRequestSetsUserAgent(c *gc.C) { + headers, _, client := s.setupLoopbackRequest() + req := RequestData{} + resp := ResponseData{ExpectedStatus: []int{http.StatusNoContent}} + err := client.JsonRequest("POST", s.Server.URL, "", &req, &resp) + c.Assert(err, gc.IsNil) + agent := headers.Get("User-Agent") + c.Check(agent, gc.Not(gc.Equals), "") + c.Check(agent, gc.Equals, gojoyentAgent()) +} + +func (s *HTTPClientTestSuite) TestBinaryRequestSetsContentLength(c *gc.C) { + headers, bodyChan, client := s.setupLoopbackRequest() + content := "binary\ncontent\n" + req := RequestData{ + ReqReader: bytes.NewBufferString(content), + ReqLength: len(content), + } + resp := ResponseData{ExpectedStatus: []int{http.StatusNoContent}} + err := client.BinaryRequest("POST", s.Server.URL, "", &req, &resp) + c.Assert(err, gc.IsNil) + encoding := headers.Get("Transfer-Encoding") + c.Check(encoding, gc.Equals, "") + length := headers.Get("Content-Length") + c.Check(length, gc.Equals, fmt.Sprintf("%d", len(content))) + body, ok := <-bodyChan + c.Assert(ok, gc.Equals, true) + c.Check(body, gc.Equals, content) +} + +func (s *HTTPClientTestSuite) TestJSONRequestSetsContentLength(c *gc.C) { + headers, bodyChan, client := s.setupLoopbackRequest() + reqMap := map[string]string{"key": "value"} + req := RequestData{ + ReqValue: reqMap, + } + resp := ResponseData{ExpectedStatus: []int{http.StatusNoContent}} + err := client.JsonRequest("POST", s.Server.URL, "", &req, &resp) + c.Assert(err, gc.IsNil) + encoding := headers.Get("Transfer-Encoding") + c.Check(encoding, gc.Equals, "") + length := headers.Get("Content-Length") + body, ok := <-bodyChan + c.Assert(ok, gc.Equals, true) + c.Check(body, gc.Not(gc.Equals), "") + c.Check(length, gc.Equals, fmt.Sprintf("%d", len(body))) +} + +func (s *HTTPSClientTestSuite) TestDefaultClientRejectSelfSigned(c *gc.C) { + _, _, client := s.setupLoopbackRequest() + req := RequestData{} + resp := ResponseData{ExpectedStatus: []int{http.StatusNoContent}} + err := client.BinaryRequest("POST", s.Server.URL, "", &req, &resp) + c.Assert(err, gc.NotNil) + c.Check(err, gc.ErrorMatches, "(.|\\n)*x509: certificate signed by unknown authority") +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/jpc/jpc.go juju-core-1.18.0/src/github.com/joyent/gocommon/jpc/jpc.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/jpc/jpc.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/jpc/jpc.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,102 @@ +// +// gocommon - Go library to interact with the JoyentCloud +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package jpc + +import ( + "fmt" + "io/ioutil" + "os" + "reflect" + "runtime" + + "github.com/joyent/gosign/auth" +) + +const ( + // Environment variables + SdcAccount = "SDC_ACCOUNT" + SdcKeyId = "SDC_KEY_ID" + SdcUrl = "SDC_URL" + MantaUser = "MANTA_USER" + MantaKeyId = "MANTA_KEY_ID" + MantaUrl = "MANTA_URL" +) + +var Locations = map[string]string{ + "us-east-1": "America/New_York", + "us-west-1": "America/Los_Angeles", + "us-sw-1": "America/Los_Angeles", + "eu-ams-1": "Europe/Amsterdam", +} + +// getConfig returns the value of the first available environment +// variable, among the given ones. +func getConfig(envVars ...string) (value string) { + value = "" + for _, v := range envVars { + value = os.Getenv(v) + if value != "" { + break + } + } + return +} + +// getUserHome returns the value of HOME environment +// variable for the user environment. +func getUserHome() string { + if runtime.GOOS == "windows" { + return os.Getenv("APPDATA") + } else { + return os.Getenv("HOME") + } +} + +// credentialsFromEnv creates and initializes the credentials from the +// environment variables. +func credentialsFromEnv(key string) (*auth.Credentials, error) { + var keyName string + if key == "" { + keyName = getUserHome() + "/.ssh/id_rsa" + } else { + keyName = key + } + privateKey, err := ioutil.ReadFile(keyName) + if err != nil { + return nil, err + } + authentication := auth.Auth{User: getConfig(SdcAccount, MantaUser), PrivateKey: string(privateKey), Algorithm: "rsa-sha256"} + + return &auth.Credentials{ + UserAuthentication: authentication, + SdcKeyId: getConfig(SdcKeyId), + SdcEndpoint: auth.Endpoint{URL: getConfig(SdcUrl)}, + MantaKeyId: getConfig(MantaKeyId), + MantaEndpoint: auth.Endpoint{URL: getConfig(MantaUrl)}, + }, nil +} + +// CompleteCredentialsFromEnv gets and verifies all the required +// authentication parameters have values in the environment. +func CompleteCredentialsFromEnv(keyName string) (cred *auth.Credentials, err error) { + cred, err = credentialsFromEnv(keyName) + if err != nil { + return nil, err + } + v := reflect.ValueOf(cred).Elem() + t := v.Type() + for i := 0; i < v.NumField(); i++ { + f := v.Field(i) + if f.String() == "" { + return nil, fmt.Errorf("Required environment variable not set for credentials attribute: %s", t.Field(i).Name) + } + } + return cred, nil +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/LICENSE juju-core-1.18.0/src/github.com/joyent/gocommon/LICENSE --- juju-core-1.17.7/src/github.com/joyent/gocommon/LICENSE 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/LICENSE 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,15 @@ +GoCommon - Go Common Library for the Joyent Public Cloud and Joyent Manta + +Copyright (c) 2013, Joyent Inc. + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU Lesser General Public License as published by the Free +Software Foundation, either version 3 of the License, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + +See both COPYING and COPYING.LESSER for the full terms of the GNU Lesser +General Public License. diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/README.md juju-core-1.18.0/src/github.com/joyent/gocommon/README.md --- juju-core-1.17.7/src/github.com/joyent/gocommon/README.md 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/README.md 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,2 @@ +gocommon +======== diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/testing/httpsuite.go juju-core-1.18.0/src/github.com/joyent/gocommon/testing/httpsuite.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/testing/httpsuite.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/testing/httpsuite.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,46 @@ +package testing + +// This package provides an HTTPSuite infrastructure that lets you bring up an +// HTTP server. The server will handle requests based on whatever Handlers are +// attached to HTTPSuite.Mux. This Mux is reset after every test case, and the +// server is shut down at the end of the test suite. + +import ( + gc "launchpad.net/gocheck" + "net/http" + "net/http/httptest" +) + +var _ = gc.Suite(&HTTPSuite{}) + +type HTTPSuite struct { + Server *httptest.Server + Mux *http.ServeMux + oldHandler http.Handler + UseTLS bool +} + +func (s *HTTPSuite) SetUpSuite(c *gc.C) { + if s.UseTLS { + s.Server = httptest.NewTLSServer(nil) + } else { + s.Server = httptest.NewServer(nil) + } +} + +func (s *HTTPSuite) SetUpTest(c *gc.C) { + s.oldHandler = s.Server.Config.Handler + s.Mux = http.NewServeMux() + s.Server.Config.Handler = s.Mux +} + +func (s *HTTPSuite) TearDownTest(c *gc.C) { + s.Mux = nil + s.Server.Config.Handler = s.oldHandler +} + +func (s *HTTPSuite) TearDownSuite(c *gc.C) { + if s.Server != nil { + s.Server.Close() + } +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/testing/httpsuite_test.go juju-core-1.18.0/src/github.com/joyent/gocommon/testing/httpsuite_test.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/testing/httpsuite_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/testing/httpsuite_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,69 @@ +package testing_test + +import ( + "crypto/tls" + "crypto/x509" + "io/ioutil" + gc "launchpad.net/gocheck" + "net/http" + "net/url" + "reflect" + "testing" + + jt "github.com/joyent/gocommon/testing" +) + +type HTTPTestSuite struct { + jt.HTTPSuite +} + +type HTTPSTestSuite struct { + jt.HTTPSuite +} + +func Test(t *testing.T) { + gc.TestingT(t) +} + +var _ = gc.Suite(&HTTPTestSuite{}) +var _ = gc.Suite(&HTTPSTestSuite{jt.HTTPSuite{UseTLS: true}}) + +type HelloHandler struct{} + +func (h *HelloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + w.WriteHeader(200) + w.Write([]byte("Hello World\n")) +} + +func (s *HTTPTestSuite) TestHelloWorld(c *gc.C) { + s.Mux.Handle("/", &HelloHandler{}) + response, err := http.Get(s.Server.URL) + c.Check(err, gc.IsNil) + content, err := ioutil.ReadAll(response.Body) + response.Body.Close() + c.Check(err, gc.IsNil) + c.Check(response.Status, gc.Equals, "200 OK") + c.Check(response.StatusCode, gc.Equals, 200) + c.Check(string(content), gc.Equals, "Hello World\n") +} + +func (s *HTTPSTestSuite) TestHelloWorldWithTLS(c *gc.C) { + s.Mux.Handle("/", &HelloHandler{}) + c.Check(s.Server.URL[:8], gc.Equals, "https://") + response, err := http.Get(s.Server.URL) + // Default http.Get fails because the cert is self-signed + c.Assert(err, gc.NotNil) + c.Assert(reflect.TypeOf(err.(*url.Error).Err), gc.Equals, reflect.TypeOf(x509.UnknownAuthorityError{})) + // Connect again with a Client that doesn't validate the cert + insecureClient := &http.Client{Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}} + response, err = insecureClient.Get(s.Server.URL) + c.Assert(err, gc.IsNil) + content, err := ioutil.ReadAll(response.Body) + response.Body.Close() + c.Check(err, gc.IsNil) + c.Check(response.Status, gc.Equals, "200 OK") + c.Check(response.StatusCode, gc.Equals, 200) + c.Check(string(content), gc.Equals, "Hello World\n") +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/version.go juju-core-1.18.0/src/github.com/joyent/gocommon/version.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/version.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/version.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,32 @@ +// +// gocommon - Go library to interact with the JoyentCloud +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package gocommon + +import ( + "fmt" +) + +type VersionNum struct { + Major int + Minor int + Micro int +} + +func (v *VersionNum) String() string { + return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Micro) +} + +var VersionNumber = VersionNum{ + Major: 0, + Minor: 1, + Micro: 0, +} + +var Version = VersionNumber.String() diff -Nru juju-core-1.17.7/src/github.com/joyent/gocommon/version_test.go juju-core-1.18.0/src/github.com/joyent/gocommon/version_test.go --- juju-core-1.17.7/src/github.com/joyent/gocommon/version_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gocommon/version_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,23 @@ +// +// gocommon - Go library to interact with the JoyentCloud +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package gocommon + +import ( + gc "launchpad.net/gocheck" +) + +type VersionTestSuite struct { +} + +var _ = gc.Suite(&VersionTestSuite{}) + +func (s *VersionTestSuite) TestStringMatches(c *gc.C) { + c.Assert(Version, gc.Equals, VersionNumber.String()) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/COPYING juju-core-1.18.0/src/github.com/joyent/gomanta/COPYING --- juju-core-1.17.7/src/github.com/joyent/gomanta/COPYING 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/COPYING 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/COPYING.LESSER juju-core-1.18.0/src/github.com/joyent/gomanta/COPYING.LESSER --- juju-core-1.17.7/src/github.com/joyent/gomanta/COPYING.LESSER 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/COPYING.LESSER 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/.gitignore juju-core-1.18.0/src/github.com/joyent/gomanta/.gitignore --- juju-core-1.17.7/src/github.com/joyent/gomanta/.gitignore 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/.gitignore 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,26 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe + +# IntelliJ files +.idea +*.iml diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/gomanta.go juju-core-1.18.0/src/github.com/joyent/gomanta/gomanta.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/gomanta.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/gomanta.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,15 @@ +/* +The gomanta package enables Go programs to interact with the Joyent Manta service. + +The gomanta package is structured as follow: + + - gomanta/localservices. This package provides local services to be used for testing. + - gomanta/manta. This package interacts with the Manta API (http://apidocs.joyent.com/manta/). + +Licensed under LGPL v3. + +Copyright (c) 2013 Joyent Inc. +Written by Daniele Stroppa + +*/ +package gomanta diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/gomanta_test.go juju-core-1.18.0/src/github.com/joyent/gomanta/gomanta_test.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/gomanta_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/gomanta_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,24 @@ +// +// gomanta - Go library to interact with Joyent Manta +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package gomanta + +import ( + gc "launchpad.net/gocheck" + "testing" +) + +func Test(t *testing.T) { + gc.TestingT(t) +} + +type GoMantaTestSuite struct { +} + +var _ = gc.Suite(&GoMantaTestSuite{}) diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/LICENSE juju-core-1.18.0/src/github.com/joyent/gomanta/LICENSE --- juju-core-1.17.7/src/github.com/joyent/gomanta/LICENSE 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/LICENSE 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,15 @@ +GoManta - Go Library for Joyent Manta + +Copyright (c) 2013, Joyent Inc. + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU Lesser General Public License as published by the Free +Software Foundation, either version 3 of the License, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + +See both COPYING and COPYING.LESSER for the full terms of the GNU Lesser +General Public License. diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/hook/service.go juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/hook/service.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/hook/service.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/hook/service.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,86 @@ +package hook + +import ( + "runtime" + "strings" +) + +type TestService struct { + ServiceControl + // Hooks to run when specified control points are reached in the service business logic. + ControlHooks map[string]ControlProcessor +} + +// ControlProcessor defines a function that is run when a specified control point is reached in the service +// business logic. The function receives the service instance so internal state can be inspected, plus for any +// arguments passed to the currently executing service function. +type ControlProcessor func(sc ServiceControl, args ...interface{}) error + +// ControlHookCleanup defines a function used to remove a control hook. +type ControlHookCleanup func() + +// ServiceControl instances allow hooks to be registered for execution at the specified point of execution. +// The control point name can be a function name or a logical execution point meaningful to the service. +// If name is "", the hook for the currently executing function is executed. +// Returns a function which can be used to remove the hook. +type ServiceControl interface { + RegisterControlPoint(name string, controller ControlProcessor) ControlHookCleanup +} + +// currentServiceMethodName returns the method executing on the service when ProcessControlHook was invoked. +func (s *TestService) currentServiceMethodName() string { + pc, _, _, ok := runtime.Caller(2) + if !ok { + panic("current method name cannot be found") + } + return unqualifiedMethodName(pc) +} + +func unqualifiedMethodName(pc uintptr) string { + f := runtime.FuncForPC(pc) + fullName := f.Name() + nameParts := strings.Split(fullName, ".") + return nameParts[len(nameParts)-1] +} + +// ProcessControlHook retrieves the ControlProcessor for the specified hook name and runs it, returning any error. +// Use it like this to invoke a hook registered for some arbitrary control point: +// if err := n.ProcessControlHook("foobar", , , ); err != nil { +// return err +// } +func (s *TestService) ProcessControlHook(hookName string, sc ServiceControl, args ...interface{}) error { + if s.ControlHooks == nil { + return nil + } + if hook, ok := s.ControlHooks[hookName]; ok { + return hook(sc, args...) + } + return nil +} + +// ProcessFunctionHook runs the ControlProcessor for the current function, returning any error. +// Use it like this: +// if err := n.ProcessFunctionHook(, , ); err != nil { +// return err +// } +func (s *TestService) ProcessFunctionHook(sc ServiceControl, args ...interface{}) error { + hookName := s.currentServiceMethodName() + return s.ProcessControlHook(hookName, sc, args...) +} + +// RegisterControlPoint assigns the specified controller to the named hook. If nil, any existing controller for the +// hook is removed. +// hookName is the name of a function on the service or some arbitrarily named control point. +func (s *TestService) RegisterControlPoint(hookName string, controller ControlProcessor) ControlHookCleanup { + if s.ControlHooks == nil { + s.ControlHooks = make(map[string]ControlProcessor) + } + if controller == nil { + delete(s.ControlHooks, hookName) + } else { + s.ControlHooks[hookName] = controller + } + return func() { + s.RegisterControlPoint(hookName, nil) + } +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/hook/service_test.go juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/hook/service_test.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/hook/service_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/hook/service_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,105 @@ +package hook + +import ( + "fmt" + gc "launchpad.net/gocheck" + "testing" +) + +func Test(t *testing.T) { + gc.TestingT(t) +} + +var _ = gc.Suite(&ServiceSuite{}) + +type ServiceSuite struct { + ts *testService +} + +func (s *ServiceSuite) SetUpTest(c *gc.C) { + s.ts = newTestService() + // This hook is called based on the function name. + s.ts.RegisterControlPoint("foo", functionControlHook) + // This hook is called based on a user specified hook name. + s.ts.RegisterControlPoint("foobar", namedControlHook) +} + +type testService struct { + TestService + label string +} + +func newTestService() *testService { + return &testService{ + TestService: TestService{ + ControlHooks: make(map[string]ControlProcessor), + }, + } +} + +func functionControlHook(s ServiceControl, args ...interface{}) error { + label := args[0].(string) + returnError := args[1].(bool) + if returnError { + return fmt.Errorf("An error occurred") + } + s.(*testService).label = label + return nil +} + +func namedControlHook(s ServiceControl, args ...interface{}) error { + s.(*testService).label = "foobar" + return nil +} + +func (s *testService) foo(label string, returnError bool) error { + if err := s.ProcessFunctionHook(s, label, returnError); err != nil { + return err + } + return nil +} + +func (s *testService) bar() error { + if err := s.ProcessControlHook("foobar", s); err != nil { + return err + } + return nil +} + +func (s *ServiceSuite) TestFunctionHookNoError(c *gc.C) { + err := s.ts.foo("success", false) + c.Assert(err, gc.IsNil) + c.Assert(s.ts.label, gc.Equals, "success") +} + +func (s *ServiceSuite) TestHookWithError(c *gc.C) { + err := s.ts.foo("success", true) + c.Assert(err, gc.Not(gc.IsNil)) + c.Assert(s.ts.label, gc.Equals, "") +} + +func (s *ServiceSuite) TestNamedHook(c *gc.C) { + err := s.ts.bar() + c.Assert(err, gc.IsNil) + c.Assert(s.ts.label, gc.Equals, "foobar") +} + +func (s *ServiceSuite) TestHookCleanup(c *gc.C) { + // Manually delete the existing control point. + s.ts.RegisterControlPoint("foo", nil) + // Register a new hook and ensure it works. + cleanup := s.ts.RegisterControlPoint("foo", functionControlHook) + err := s.ts.foo("cleanuptest", false) + c.Assert(err, gc.IsNil) + c.Assert(s.ts.label, gc.Equals, "cleanuptest") + // Use the cleanup func to remove the hook and check the result. + cleanup() + err = s.ts.foo("again", false) + c.Assert(err, gc.IsNil) + c.Assert(s.ts.label, gc.Equals, "cleanuptest") + // Ensure that only the specified hook was removed and the other remaining one still works. + err = s.ts.bar() + c.Assert(err, gc.IsNil) + c.Assert(s.ts.label, gc.Equals, "foobar") + +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/localservice.go juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/localservice.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/localservice.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/localservice.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,45 @@ +// +// gomanta - Go library to interact with Joyent Manta +// +// Double testing service +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package localservices + +import ( + "crypto/rand" + "fmt" + "io" + "net/http" + + "github.com/joyent/gomanta/localservices/hook" +) + +// An HttpService provides the HTTP API for a service double. +type HttpService interface { + SetupHTTP(mux *http.ServeMux) +} + +// A ServiceInstance is an Joyent Cloud service, one of manta or cloudapi. +type ServiceInstance struct { + hook.TestService + Scheme string + Hostname string + UserAccount string +} + +// NewUUID generates a random UUID according to RFC 4122 +func NewUUID() (string, error) { + uuid := make([]byte, 16) + n, err := io.ReadFull(rand.Reader, uuid) + if n != len(uuid) || err != nil { + return "", err + } + uuid[8] = uuid[8]&^0xc0 | 0x80 + uuid[6] = uuid[6]&^0xf0 | 0x40 + return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/manta/service.go juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/manta/service.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/manta/service.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/manta/service.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,505 @@ +// +// gomanta - Go library to interact with Joyent Manta +// +// Manta double testing service - internal direct API implementation +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package manta + +import ( + "encoding/json" + "fmt" + "net/url" + "sort" + "strings" + "time" + + "github.com/joyent/gomanta/localservices" + "github.com/joyent/gomanta/manta" +) + +const ( + storagePrefix = "/%s/stor/%s" + jobsPrefix = "/%s/jobs/%s" + separator = "/" + typeDirectory = "directory" + typeObject = "object" +) + +type Manta struct { + localservices.ServiceInstance + objects map[string]manta.Entry + objectsData map[string][]byte + jobs map[string]*manta.Job +} + +func New(serviceURL, userAccount string) *Manta { + URL, err := url.Parse(serviceURL) + if err != nil { + panic(err) + } + hostname := URL.Host + if !strings.HasSuffix(hostname, separator) { + hostname += separator + } + + mantaDirectories := make(map[string]manta.Entry) + + path := fmt.Sprintf("/%s", userAccount) + mantaDirectories[path] = createDirectory(userAccount) + path = fmt.Sprintf("/%s/stor", userAccount) + mantaDirectories[path] = createDirectory("stor") + path = fmt.Sprintf("/%s/jobs", userAccount) + mantaDirectories[path] = createDirectory("jobs") + + mantaService := &Manta{ + objects: mantaDirectories, + objectsData: make(map[string][]byte), + jobs: make(map[string]*manta.Job), + ServiceInstance: localservices.ServiceInstance{ + Scheme: URL.Scheme, + Hostname: hostname, + UserAccount: userAccount, + }, + } + + return mantaService +} + +func createDirectory(directoryName string) manta.Entry { + return manta.Entry{ + Name: directoryName, + Type: typeDirectory, + Mtime: time.Now().Format(time.RFC3339), + } +} + +func createJobObject(objName string, objData []byte) (manta.Entry, error) { + etag, err := localservices.NewUUID() + if err == nil { + return manta.Entry{ + Name: objName, + Type: typeObject, + Mtime: time.Now().Format(time.RFC3339), + Etag: etag, + Size: len(objData), + }, nil + } + + return manta.Entry{}, err +} + +func (m *Manta) IsObject(name string) bool { + _, exist := m.objectsData[name] + return exist +} + +func (m *Manta) IsDirectory(name string) bool { + _, exist := m.objects[name] + return !m.IsObject(name) && exist +} + +// Directories APIs +func (m *Manta) ListDirectory(path, marker string, limit int) ([]manta.Entry, error) { + if err := m.ProcessFunctionHook(m, path, marker, limit); err != nil { + return nil, err + } + + realPath := fmt.Sprintf(storagePrefix, m.ServiceInstance.UserAccount, path) + + if limit == 0 { + limit = 256 + } + + if _, ok := m.objects[realPath]; !ok { + return nil, fmt.Errorf("%s was not found", realPath) + } + + var sortedKeys []string + for k := range m.objects { + sortedKeys = append(sortedKeys, k) + } + sort.Strings(sortedKeys) + + if !strings.HasSuffix(realPath, separator) { + realPath = realPath + separator + } + + var entries []manta.Entry + var entriesKeys []string +sortedLoop: + for _, key := range sortedKeys { + if strings.Contains(key, realPath) { + for _, k := range entriesKeys { + if strings.Contains(key, k) { + continue sortedLoop + } + } + entriesKeys = append(entriesKeys, key) + } + } + + for _, k := range entriesKeys { + if marker != "" && marker > k[strings.LastIndex(k, "/")+1:] { + continue + } + entries = append(entries, m.objects[k]) + if len(entries) >= limit { + break + } + } + + return entries, nil +} + +func getParentDirs(userAccount, path string) []string { + var parents []string + + tokens := strings.Split(path, separator) + for index, _ := range tokens { + parents = append(parents, fmt.Sprintf(storagePrefix, userAccount, strings.Join(tokens[:(index+1)], separator))) + } + + return parents +} + +func (m *Manta) PutDirectory(path string) error { + if err := m.ProcessFunctionHook(m, path); err != nil { + return err + } + + realPath := fmt.Sprintf(storagePrefix, m.ServiceInstance.UserAccount, path) + + // Check if parent dirs exist + if strings.Contains(path, separator) { + ppath := path[:strings.LastIndex(path, separator)] + parents := getParentDirs(m.ServiceInstance.UserAccount, ppath) + for _, p := range parents { + if _, ok := m.objects[p]; !ok { + return fmt.Errorf("%s was not found", p) + } + } + } + + dir := manta.Entry{ + Name: path[(strings.LastIndex(path, separator) + 1):], + Type: typeDirectory, + Mtime: time.Now().Format(time.RFC3339), + } + + m.objects[realPath] = dir + + return nil +} + +func (m *Manta) DeleteDirectory(path string) error { + if err := m.ProcessFunctionHook(m, path); err != nil { + return err + } + + realPath := fmt.Sprintf(storagePrefix, m.ServiceInstance.UserAccount, path) + + // Check if empty + ppath := realPath + separator + for k, _ := range m.objects { + if strings.Contains(k, ppath) { + return ErrBadRequest + } + } + + delete(m.objects, realPath) + + return nil +} + +// Objects APIs +func (m *Manta) PutObject(path, objName string, objData []byte) error { + if err := m.ProcessFunctionHook(m, path, objName, objData); err != nil { + return err + } + + realPath := fmt.Sprintf(storagePrefix, m.ServiceInstance.UserAccount, path) + + // Check if parent dirs exist + parents := getParentDirs(m.ServiceInstance.UserAccount, path) + for _, p := range parents { + if _, ok := m.objects[p]; !ok { + return fmt.Errorf("%s was not found", realPath) + } + } + + etag, err := localservices.NewUUID() + if err != nil { + return err + } + + obj := manta.Entry{ + Name: objName, + Type: typeObject, + Mtime: time.Now().Format(time.RFC3339), + Etag: etag, + Size: len(objData), + } + + objId := fmt.Sprintf("%s/%s", realPath, objName) + m.objects[objId] = obj + m.objectsData[objId] = objData + + return nil +} + +func (m *Manta) GetObject(objPath string) ([]byte, error) { + if err := m.ProcessFunctionHook(m, objPath); err != nil { + return nil, err + } + + objId := fmt.Sprintf(storagePrefix, m.ServiceInstance.UserAccount, objPath) + if _, ok := m.objects[objId]; ok { + // TODO: Headers! + return m.objectsData[objId], nil + } + + return nil, fmt.Errorf("%s was not found", objId) +} + +func (m *Manta) DeleteObject(objPath string) error { + if err := m.ProcessFunctionHook(m, objPath); err != nil { + return err + } + + objId := fmt.Sprintf(storagePrefix, m.ServiceInstance.UserAccount, objPath) + if _, ok := m.objects[objId]; ok { + delete(m.objects, objId) + delete(m.objectsData, objId) + + return nil + } + return fmt.Errorf("%s was not found", objId) +} + +// Link APIs +func (m *Manta) PutSnapLink(path, linkName, location string) error { + if err := m.ProcessFunctionHook(m, path, linkName, location); err != nil { + return err + } + + realPath := fmt.Sprintf(storagePrefix, m.ServiceInstance.UserAccount, path) + + // Check if parent dirs exist + parents := getParentDirs(m.ServiceInstance.UserAccount, path) + for _, p := range parents { + if _, ok := m.objects[p]; !ok { + return fmt.Errorf("%s was not found", realPath) + } + } + + // Check if location exist + if _, ok := m.objects[location]; !ok { + return fmt.Errorf("%s was not found", location) + } + + etag, err := localservices.NewUUID() + if err != nil { + return err + } + + obj := manta.Entry{ + Name: linkName, + Type: typeObject, + Mtime: time.Now().Format(time.RFC3339), + Etag: etag, + Size: len(m.objectsData[location]), + } + + objId := fmt.Sprintf("%s/%s", realPath, linkName) + m.objects[objId] = obj + m.objectsData[objId] = m.objectsData[location] + + return nil +} + +// Job APIs +func (m *Manta) ListJobs(live bool) ([]manta.Entry, error) { + var jobs []manta.Entry + + if err := m.ProcessFunctionHook(m, live); err != nil { + return nil, err + } + + for _, job := range m.jobs { + if live && (job.Cancelled || job.TimeDone != "") { + continue + } + jobKey := fmt.Sprintf(jobsPrefix, m.ServiceInstance.UserAccount, job.Id) + jobs = append(jobs, m.objects[jobKey]) + } + return jobs, nil +} + +func (m *Manta) CreateJob(job []byte) (string, error) { + if err := m.ProcessFunctionHook(m, job); err != nil { + return "", err + } + + jsonJob := new(manta.Job) + err := json.Unmarshal(job, jsonJob) + if err != nil { + return "", err + } + jobId, err := localservices.NewUUID() + if err != nil { + return "", err + } + jsonJob.Id = jobId + jsonJob.State = "running" + jsonJob.Cancelled = false + jsonJob.InputDone = false + jsonJob.TimeCreated = time.Now().Format(time.RFC3339) + + //create directories + realPath := fmt.Sprintf(jobsPrefix, m.ServiceInstance.UserAccount, jobId) + m.objects[realPath] = createDirectory(jobId) + realPath = fmt.Sprintf(jobsPrefix, m.ServiceInstance.UserAccount, fmt.Sprintf("%s/stor", jobId)) + m.objects[realPath] = createDirectory("stor") + + m.jobs[jsonJob.Id] = jsonJob + return fmt.Sprintf("/%s/jobs/%s", m.ServiceInstance.UserAccount, jobId), nil +} + +func (m *Manta) GetJob(id string) (*manta.Job, error) { + if err := m.ProcessFunctionHook(m, id); err != nil { + return nil, err + } + + if job, ok := m.jobs[id]; ok { + return job, nil + } + return nil, fmt.Errorf("/%s/jobs/%s/job.json was not found", m.ServiceInstance.UserAccount, id) +} + +func (m *Manta) CancelJob(id string) error { + if err := m.ProcessFunctionHook(m, id); err != nil { + return err + } + + if job, ok := m.jobs[id]; ok { + if !job.InputDone { + job.Cancelled = true + job.InputDone = true + job.TimeDone = time.Now().Format(time.RFC3339) + } else { + return fmt.Errorf("/%s/jobs/%s/live/cancel does not exist", m.ServiceInstance.UserAccount, id) + } + return nil + } + + return fmt.Errorf("/%s/jobs/%s/job.json was not found", m.ServiceInstance.UserAccount, id) +} + +func (m *Manta) AddJobInputs(id string, jobInputs []byte) error { + if err := m.ProcessFunctionHook(m, id); err != nil { + return err + } + + if job, ok := m.jobs[id]; ok { + var err error + if !job.InputDone { + // add inputs + objId := fmt.Sprintf("/%s/jobs/%s/in.txt", m.ServiceInstance.UserAccount, id) + m.objects[objId], err = createJobObject("in.txt", jobInputs) + if err != nil { + return err + } + m.objectsData[objId] = jobInputs + + return nil + } else { + return fmt.Errorf("/%s/jobs/%s/live/in does not exist", m.ServiceInstance.UserAccount, id) + } + } + + return fmt.Errorf("/%s/jobs/%s/job.json was not found", m.ServiceInstance.UserAccount, id) +} + +func (m *Manta) EndJobInput(id string) error { + if err := m.ProcessFunctionHook(m, id); err != nil { + return err + } + + if job, ok := m.jobs[id]; ok { + if !job.InputDone { + job.InputDone = true + } else { + return fmt.Errorf("/%s/jobs/%s/live/in/end does not exist", m.ServiceInstance.UserAccount, id) + } + return nil + } + + return fmt.Errorf("/%s/jobs/%s/job.json was not found", m.ServiceInstance.UserAccount, id) +} + +func (m *Manta) GetJobOutput(id string) (string, error) { + if err := m.ProcessFunctionHook(m, id); err != nil { + return "", err + } + + if job, ok := m.jobs[id]; ok { + index := len(job.Phases) - 1 + phaseType := job.Phases[index].Type + outputId, err := localservices.NewUUID() + if err != nil { + return "", err + } + jobOutput := fmt.Sprintf("/%s/jobs/%s/stor/%s.%d.%s", m.ServiceInstance.UserAccount, id, phaseType, index, outputId) + + return jobOutput, nil + } + + return "", fmt.Errorf("/%s/jobs/%s/job.json was not found", m.ServiceInstance.UserAccount, id) +} + +func (m *Manta) GetJobInput(id string) (string, error) { + if err := m.ProcessFunctionHook(m, id); err != nil { + return "", err + } + + if _, ok := m.jobs[id]; ok { + + objId := fmt.Sprintf("/%s/jobs/%s/in.txt", m.ServiceInstance.UserAccount, id) + if _, ok := m.objects[objId]; ok { + return string(m.objectsData[objId]), nil + } + + return "", nil + } + + return "", fmt.Errorf("/%s/jobs/%s/job.json was not found", m.ServiceInstance.UserAccount, id) +} + +func (m *Manta) GetJobFailures(id string) (string, error) { + if err := m.ProcessFunctionHook(m, id); err != nil { + return "", err + } + + if _, ok := m.jobs[id]; ok { + return "", nil + } + + return "", fmt.Errorf("/%s/jobs/%s/job.json was not found", m.ServiceInstance.UserAccount, id) +} + +func (m *Manta) GetJobErrors(id string) ([]manta.JobError, error) { + if err := m.ProcessFunctionHook(m, id); err != nil { + return nil, err + } + + if _, ok := m.jobs[id]; ok { + return nil, nil + } + + return nil, fmt.Errorf("/%s/jobs/%s/job.json was not found", m.ServiceInstance.UserAccount, id) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/manta/service_http.go juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/manta/service_http.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/manta/service_http.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/manta/service_http.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,392 @@ +// +// gomanta - Go library to interact with Joyent Manta +// +// Manta double testing service - HTTP API implementation +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package manta + +import ( + //"bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + "strings" + + "github.com/joyent/gomanta/manta" +) + +// ErrorResponse defines a single HTTP error response. +type ErrorResponse struct { + Code int + Body string + contentType string + errorText string + headers map[string]string + manta *Manta +} + +var ( + ErrNotAllowed = &ErrorResponse{ + http.StatusMethodNotAllowed, + "Method is not allowed", + "text/plain; charset=UTF-8", + "MethodNotAllowedError", + nil, + nil, + } + ErrNotFound = &ErrorResponse{ + http.StatusNotFound, + "Resource Not Found", + "text/plain; charset=UTF-8", + "NotFoundError", + nil, + nil, + } + ErrBadRequest = &ErrorResponse{ + http.StatusBadRequest, + "Malformed request url", + "text/plain; charset=UTF-8", + "BadRequestError", + nil, + nil, + } +) + +func (e *ErrorResponse) Error() string { + return e.errorText +} + +func (e *ErrorResponse) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if e.contentType != "" { + w.Header().Set("Content-Type", e.contentType) + } + body := e.Body + if e.headers != nil { + for h, v := range e.headers { + w.Header().Set(h, v) + } + } + // workaround for https://code.google.com/p/go/issues/detail?id=4454 + w.Header().Set("Content-Length", strconv.Itoa(len(body))) + if e.Code != 0 { + w.WriteHeader(e.Code) + } + if len(body) > 0 { + w.Write([]byte(body)) + } +} + +type mantaHandler struct { + manta *Manta + method func(m *Manta, w http.ResponseWriter, r *http.Request) error +} + +func (h *mantaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + // handle trailing slash in the path + if strings.HasSuffix(path, "/") && path != "/" { + ErrNotFound.ServeHTTP(w, r) + return + } + err := h.method(h.manta, w, r) + if err == nil { + return + } + var resp http.Handler + resp, _ = err.(http.Handler) + if resp == nil { + resp = &ErrorResponse{ + http.StatusInternalServerError, + `{"internalServerError":{"message":"Unkown Error",code:500}}`, + "application/json", + err.Error(), + nil, + h.manta, + } + } + resp.ServeHTTP(w, r) +} + +func writeResponse(w http.ResponseWriter, code int, body []byte) { + // workaround for https://code.google.com/p/go/issues/detail?id=4454 + w.Header().Set("Content-Length", strconv.Itoa(len(body))) + w.WriteHeader(code) + w.Write(body) +} + +// sendJSON sends the specified response serialized as JSON. +func sendJSON(code int, resp interface{}, w http.ResponseWriter, r *http.Request) error { + data, err := json.Marshal(resp) + if err != nil { + return err + } + writeResponse(w, code, data) + return nil +} + +func getJobId(url string) string { + tokens := strings.Split(url, "/") + return tokens[3] +} + +func (manta *Manta) handler(method func(m *Manta, w http.ResponseWriter, r *http.Request) error) http.Handler { + return &mantaHandler{manta, method} +} + +// handleStorage handles the storage HTTP API. +func (m *Manta) handleStorage(w http.ResponseWriter, r *http.Request) error { + prefix := fmt.Sprintf("/%s/stor/", m.ServiceInstance.UserAccount) + object := strings.TrimPrefix(r.URL.Path, prefix) + switch r.Method { + case "GET": + if m.IsObject(r.URL.Path) { + var resp []byte + //GetObject + obj, err := m.GetObject(object) + if err != nil { + return err + } + if obj == nil { + obj = []byte{} + } + // Check if request came from client or signed URL + //if r.URL.RawQuery != "" { + // d := json.NewDecoder(bytes.NewReader(obj)) + // d.Decode(&resp) + //} else { + resp = obj + //} + // not using sendJson to avoid double json encoding + writeResponse(w, http.StatusOK, resp) + return nil + } else if m.IsDirectory(r.URL.Path) { + //ListDirectory + var ( + marker string + limit int + ) + opts := &manta.ListDirectoryOpts{} + body, errB := ioutil.ReadAll(r.Body) + if errB != nil { + return errB + } + if len(body) > 0 { + if errJ := json.Unmarshal(body, opts); errJ != nil { + return errJ + } + marker = opts.Marker + limit = opts.Limit + } + entries, err := m.ListDirectory(object, marker, limit) + if err != nil { + return err + } + if entries == nil { + entries = []manta.Entry{} + } + resp := entries + return sendJSON(http.StatusOK, resp, w, r) + } else { + return ErrNotFound + } + case "POST": + return ErrNotAllowed + case "PUT": + if r.Header.Get("Content-Type") == "application/json; type=directory" { + // PutDirectory + err := m.PutDirectory(object) + if err != nil { + return err + } + return sendJSON(http.StatusNoContent, nil, w, r) + } else if r.Header.Get("Location") != "" { + // PutSnaplink + path := object[:strings.LastIndex(object, "/")] + objName := object[strings.LastIndex(object, "/")+1:] + err := m.PutSnapLink(path, objName, r.Header.Get("Location")) + if err != nil { + return err + } + return sendJSON(http.StatusNoContent, nil, w, r) + } else { + // PutObject + path := object[:strings.LastIndex(object, "/")] + objName := object[strings.LastIndex(object, "/")+1:] + defer r.Body.Close() + objectData, err := ioutil.ReadAll(r.Body) + if err != nil { + return err + } + err = m.PutObject(path, objName, objectData) + if err != nil { + return err + } + return sendJSON(http.StatusNoContent, nil, w, r) + } + case "DELETE": + if m.IsObject(r.URL.Path) { + //DeleteObject + err := m.DeleteObject(object) + if err != nil { + return err + } + return sendJSON(http.StatusNoContent, nil, w, r) + } else if m.IsDirectory(r.URL.Path) { + //DeleteDirectory + err := m.DeleteDirectory(object) + if err != nil { + return err + } + return sendJSON(http.StatusNoContent, nil, w, r) + } else { + return ErrNotFound + } + } + return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path) +} + +// handleJob handles the Job HTTP API. +func (m *Manta) handleJobs(w http.ResponseWriter, r *http.Request) error { + var live = false + switch r.Method { + case "GET": + if strings.HasSuffix(r.URL.Path, "jobs") { + // ListJobs + if state := r.FormValue("state"); state == "running" { + live = true + } + jobs, err := m.ListJobs(live) + if err != nil { + return err + } + if len(jobs) == 0 { + jobs = []manta.Entry{} + } + resp := jobs + return sendJSON(http.StatusOK, resp, w, r) + } else if strings.HasSuffix(r.URL.Path, "status") { + // GetJob + job, err := m.GetJob(getJobId(r.URL.Path)) + if err != nil { + return err + } + if job == nil { + job = &manta.Job{} + } + resp := job + return sendJSON(http.StatusOK, resp, w, r) + } else if strings.HasSuffix(r.URL.Path, "out") { + // GetJobOutput + out, err := m.GetJobOutput(getJobId(r.URL.Path)) + if err != nil { + return err + } + resp := out + return sendJSON(http.StatusOK, resp, w, r) + } else if strings.HasSuffix(r.URL.Path, "in") { + // GetJobInput + in, err := m.GetJobInput(getJobId(r.URL.Path)) + if err != nil { + return err + } + resp := in + return sendJSON(http.StatusOK, resp, w, r) + } else if strings.HasSuffix(r.URL.Path, "fail") { + // GetJobFailures + fail, err := m.GetJobFailures(getJobId(r.URL.Path)) + if err != nil { + return err + } + resp := fail + return sendJSON(http.StatusOK, resp, w, r) + } else if strings.HasSuffix(r.URL.Path, "err") { + // GetJobErrors + jobErr, err := m.GetJobErrors(getJobId(r.URL.Path)) + if err != nil { + return err + } + //if jobErr == nil { + // jobErr = []manta.JobError{} + //} + resp := jobErr + return sendJSON(http.StatusOK, resp, w, r) + } else { + return ErrNotAllowed + } + case "POST": + if strings.HasSuffix(r.URL.Path, "jobs") { + body, errb := ioutil.ReadAll(r.Body) + if errb != nil { + return errb + } + if len(body) == 0 { + return ErrBadRequest + } + jobId, err := m.CreateJob(body) + if err != nil { + return err + } + w.Header().Add("Location", jobId) + return sendJSON(http.StatusCreated, nil, w, r) + } else if strings.HasSuffix(r.URL.Path, "cancel") { + // CancelJob + err := m.CancelJob(getJobId(r.URL.Path)) + if err != nil { + return err + } + return sendJSON(http.StatusAccepted, nil, w, r) + } else if strings.HasSuffix(r.URL.Path, "end") { + // EndJobInputs + err := m.EndJobInput(getJobId(r.URL.Path)) + if err != nil { + return err + } + return sendJSON(http.StatusAccepted, nil, w, r) + } else if strings.HasSuffix(r.URL.Path, "in") { + // AddJobInputs + body, err := ioutil.ReadAll(r.Body) + if err != nil { + return err + } + if len(body) == 0 { + return ErrBadRequest + } + err = m.AddJobInputs(getJobId(r.URL.Path), body) + if err != nil { + return err + } + return sendJSON(http.StatusNoContent, nil, w, r) + } else { + return ErrNotAllowed + } + case "PUT": + return ErrNotAllowed + case "DELETE": + return ErrNotAllowed + } + return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path) +} + +// setupHTTP attaches all the needed handlers to provide the HTTP API. +func (m *Manta) SetupHTTP(mux *http.ServeMux) { + handlers := map[string]http.Handler{ + "/": ErrNotFound, + "/$user/": ErrBadRequest, + "/$user/stor": m.handler((*Manta).handleStorage), + "/$user/jobs": m.handler((*Manta).handleJobs), + } + for path, h := range handlers { + path = strings.Replace(path, "$user", m.ServiceInstance.UserAccount, 1) + if !strings.HasSuffix(path, "/") { + mux.Handle(path+"/", h) + } + mux.Handle(path, h) + } +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/manta/service_http_test.go juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/manta/service_http_test.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/manta/service_http_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/manta/service_http_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,476 @@ +// +// gomanta - Go library to interact with Joyent Manta +// +// Manta double testing service - HTTP API tests +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package manta_test + +import ( + "bytes" + "encoding/json" + "io/ioutil" + gc "launchpad.net/gocheck" + "net/http" + "strconv" + "strings" + + "fmt" + "github.com/joyent/gocommon/testing" + lm "github.com/joyent/gomanta/localservices/manta" + "github.com/joyent/gomanta/manta" +) + +type MantaHTTPSuite struct { + testing.HTTPSuite + service *lm.Manta +} + +var _ = gc.Suite(&MantaHTTPSuite{}) + +type MantaHTTPSSuite struct { + testing.HTTPSuite + service *lm.Manta +} + +var _ = gc.Suite(&MantaHTTPSSuite{HTTPSuite: testing.HTTPSuite{UseTLS: true}}) + +const ( + fakeStorPrefix = "/fakeuser/stor" + fakeJobsPrefix = "/fakeuser/jobs" +) + +func (s *MantaHTTPSuite) SetUpSuite(c *gc.C) { + s.HTTPSuite.SetUpSuite(c) + c.Assert(s.Server.URL[:7], gc.Equals, "http://") + s.service = lm.New(s.Server.URL, "fakeuser") +} + +func (s *MantaHTTPSuite) TearDownSuite(c *gc.C) { + s.HTTPSuite.TearDownSuite(c) +} + +func (s *MantaHTTPSuite) SetUpTest(c *gc.C) { + s.HTTPSuite.SetUpTest(c) + s.service.SetupHTTP(s.Mux) +} + +func (s *MantaHTTPSuite) TearDownTest(c *gc.C) { + s.HTTPSuite.TearDownTest(c) +} + +// assertJSON asserts the passed http.Response's body can be +// unmarshalled into the given expected object, populating it with the +// successfully parsed data. +func assertJSON(c *gc.C, resp *http.Response, expected interface{}) { + body, err := ioutil.ReadAll(resp.Body) + defer resp.Body.Close() + c.Assert(err, gc.IsNil) + err = json.Unmarshal(body, &expected) + c.Assert(err, gc.IsNil) +} + +// assertBody asserts the passed http.Response's body matches the +// expected response, replacing any variables in the expected body. +func assertBody(c *gc.C, resp *http.Response, expected *lm.ErrorResponse) { + body, err := ioutil.ReadAll(resp.Body) + defer resp.Body.Close() + c.Assert(err, gc.IsNil) + expBody := expected.Body + // cast to string for easier asserts debugging + c.Assert(string(body), gc.Equals, string(expBody)) +} + +// sendRequest constructs an HTTP request from the parameters and +// sends it, returning the response or an error. +func (s *MantaHTTPSuite) sendRequest(method, path string, body []byte, headers http.Header) (*http.Response, error) { + if headers == nil { + headers = make(http.Header) + } + requestURL := "http://" + s.service.Hostname + strings.TrimLeft(path, "/") + req, err := http.NewRequest(method, requestURL, bytes.NewReader(body)) + if err != nil { + return nil, err + } + req.Close = true + for header, values := range headers { + for _, value := range values { + req.Header.Add(header, value) + } + } + // workaround for https://code.google.com/p/go/issues/detail?id=4454 + req.Header.Set("Content-Length", strconv.Itoa(len(body))) + return http.DefaultClient.Do(req) +} + +// jsonRequest serializes the passed body object to JSON and sends a +// the request with authRequest(). +func (s *MantaHTTPSuite) jsonRequest(method, path string, body interface{}, headers http.Header) (*http.Response, error) { + jsonBody, err := json.Marshal(body) + if err != nil { + return nil, err + } + return s.sendRequest(method, path, jsonBody, headers) +} + +// Helpers +func (s *MantaHTTPSuite) createDirectory(c *gc.C, path string) { + reqHeaders := make(http.Header) + reqHeaders.Set("Content-Type", "application/json; type=directory") + resp, err := s.sendRequest("PUT", fmt.Sprintf("%s/%s", fakeStorPrefix, path), nil, reqHeaders) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusNoContent) +} + +func (s *MantaHTTPSuite) createObject(c *gc.C, objPath string) { + resp, err := s.sendRequest("PUT", fmt.Sprintf("%s/%s", fakeStorPrefix, objPath), []byte(object), nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusNoContent) +} + +func (s *MantaHTTPSuite) delete(c *gc.C, path string) { + resp, err := s.sendRequest("DELETE", fmt.Sprintf("%s/%s", fakeStorPrefix, path), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusNoContent) +} + +func getTestJob(c *gc.C, jobName string) []byte { + phases := []manta.Phase{{Type: "map", Exec: "wc", Init: ""}, {Type: "reduce", Exec: "awk '{ l += $1; w += $2; c += $3 } END { print l, w, c }'", Init: ""}} + opts := manta.CreateJobOpts{Name: jobName, Phases: phases} + optsByte, err := json.Marshal(opts) + c.Assert(err, gc.IsNil) + return optsByte +} + +func (s *MantaHTTPSuite) createJob(c *gc.C, jobName string) string { + resp, err := s.sendRequest("POST", fakeJobsPrefix, getTestJob(c, jobName), nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusCreated) + return strings.Split(resp.Header.Get("Location"), "/")[3] +} + +// SimpleTest defines a simple request without a body and expected response. +type SimpleTest struct { + method string + url string + headers http.Header + expect *lm.ErrorResponse +} + +func (s *MantaHTTPSuite) simpleTests() []SimpleTest { + var simpleTests = []SimpleTest{ + { + method: "GET", + url: "/", + headers: make(http.Header), + expect: lm.ErrNotFound, + }, + { + method: "POST", + url: "/", + headers: make(http.Header), + expect: lm.ErrNotFound, + }, + { + method: "DELETE", + url: "/", + headers: make(http.Header), + expect: lm.ErrNotFound, + }, + { + method: "PUT", + url: "/", + headers: make(http.Header), + expect: lm.ErrNotFound, + }, + { + method: "GET", + url: "/any", + headers: make(http.Header), + expect: lm.ErrNotFound, + }, + { + method: "POST", + url: "/any", + headers: make(http.Header), + expect: lm.ErrNotFound, + }, + { + method: "DELETE", + url: "/any", + headers: make(http.Header), + expect: lm.ErrNotFound, + }, + { + method: "PUT", + url: "/any", + headers: make(http.Header), + expect: lm.ErrNotFound, + }, + { + method: "POST", + url: "/fakeuser/stor", + headers: make(http.Header), + expect: lm.ErrNotAllowed, + }, + { + method: "DELETE", + url: "/fakeuser/jobs", + headers: make(http.Header), + expect: lm.ErrNotAllowed, + }, + { + method: "PUT", + url: "/fakeuser/jobs", + headers: make(http.Header), + expect: lm.ErrNotAllowed, + }, + } + return simpleTests +} + +func (s *MantaHTTPSuite) TestSimpleRequestTests(c *gc.C) { + simpleTests := s.simpleTests() + for i, t := range simpleTests { + c.Logf("#%d. %s %s -> %d", i, t.method, t.url, t.expect.Code) + if t.headers == nil { + t.headers = make(http.Header) + } + var ( + resp *http.Response + err error + ) + resp, err = s.sendRequest(t.method, t.url, nil, t.headers) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, t.expect.Code) + assertBody(c, resp, t.expect) + } +} + +// Storage API +func (s *MantaHTTPSuite) TestPutDirectory(c *gc.C) { + s.createDirectory(c, "test") + s.delete(c, "test") +} + +func (s *MantaHTTPSuite) TestPutDirectoryWithParent(c *gc.C) { + s.createDirectory(c, "test") + defer s.delete(c, "test") + s.createDirectory(c, "test/innerdir") + + s.delete(c, "test/innerdir") +} + +func (s *MantaHTTPSuite) TestListDirectory(c *gc.C) { + var expected []manta.Entry + s.createDirectory(c, "test") + defer s.delete(c, "test") + s.createObject(c, "test/object") + defer s.delete(c, "test/object") + + resp, err := s.sendRequest("GET", "/fakeuser/stor/test", nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(expected, gc.HasLen, 1) +} + +func (s *MantaHTTPSuite) TestListDirectoryWithOpts(c *gc.C) { + var expected []manta.Entry + s.createDirectory(c, "dir") + defer s.delete(c, "dir") + s.createObject(c, "dir/object1") + defer s.delete(c, "dir/object1") + s.createObject(c, "dir/object2") + defer s.delete(c, "dir/object2") + s.createObject(c, "dir/object3") + defer s.delete(c, "dir/object3") + + opts := manta.ListDirectoryOpts{Limit: 1, Marker: "object2"} + optsByte, errB := json.Marshal(opts) + c.Assert(errB, gc.IsNil) + resp, err := s.sendRequest("GET", "/fakeuser/stor/dir", optsByte, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(expected, gc.HasLen, 1) +} + +func (s *MantaHTTPSuite) TestDeleteDirectory(c *gc.C) { + s.createDirectory(c, "test") + defer s.delete(c, "test") + s.createDirectory(c, "test/innerdir") + + s.delete(c, "test/innerdir") +} + +func (s *MantaHTTPSuite) TestPutObject(c *gc.C) { + s.createDirectory(c, "dir") + defer s.delete(c, "dir") + s.createObject(c, "dir/object") + + s.delete(c, "dir/object") +} + +func (s *MantaHTTPSuite) TestGetObject(c *gc.C) { + s.createDirectory(c, "dir") + defer s.delete(c, "dir") + s.createObject(c, "dir/object") + defer s.delete(c, "dir/object") + + resp, err := s.sendRequest("GET", "/fakeuser/stor/dir/object", nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + // TODO: assert for headers +} + +func (s *MantaHTTPSuite) TestDeleteObject(c *gc.C) { + s.createDirectory(c, "dir") + defer s.delete(c, "dir") + s.createObject(c, "dir/object") + + s.delete(c, "dir/object") +} + +func (s *MantaHTTPSuite) TestPutSnaplink(c *gc.C) { + s.createDirectory(c, "dir") + defer s.delete(c, "dir") + s.createObject(c, "dir/object") + defer s.delete(c, "dir/object") + defer s.delete(c, "dir/link") + + reqHeaders := make(http.Header) + reqHeaders.Set("Location", "/fakeuser/stor/dir/object") + resp, err := s.sendRequest("PUT", "/fakeuser/stor/dir/link", nil, reqHeaders) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusNoContent) +} + +// Jobs API +func (s *MantaHTTPSuite) TestCreateJob(c *gc.C) { + s.createJob(c, "test-job") +} + +func (s *MantaHTTPSuite) TestListAllJobs(c *gc.C) { + var expected []manta.Entry + s.createJob(c, "test-job") + + resp, err := s.sendRequest("GET", fakeJobsPrefix, nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + //c.Assert(len(expected), gc.Equals, 2) +} + +func (s *MantaHTTPSuite) TestListLiveJobs(c *gc.C) { + var expected []manta.Entry + s.createJob(c, "test-job") + + resp, err := s.sendRequest("GET", fmt.Sprintf("%s?state=running", fakeJobsPrefix), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + //c.Assert(len(expected), gc.Equals, 1) +} + +func (s *MantaHTTPSuite) TestCancelJob(c *gc.C) { + jobId := s.createJob(c, "test-job") + + url := fmt.Sprintf("%s/%s/live/cancel", fakeJobsPrefix, jobId) + resp, err := s.sendRequest("POST", url, nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted) +} + +func (s *MantaHTTPSuite) TestAddJobInputs(c *gc.C) { + var inputs = `/fakeuser/stor/testjob/obj1 +/fakeuser/stor/testjob/obj2 +` + s.createDirectory(c, "testjob") + s.createObject(c, "testjob/obj1") + s.createObject(c, "testjob/obj2") + jobId := s.createJob(c, "test-job") + + url := fmt.Sprintf("%s/%s/live/in", fakeJobsPrefix, jobId) + resp, err := s.sendRequest("POST", url, []byte(inputs), nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusNoContent) +} + +func (s *MantaHTTPSuite) TestEndJobInputs(c *gc.C) { + jobId := s.createJob(c, "test-job") + + url := fmt.Sprintf("%s/%s/live/in/end", fakeJobsPrefix, jobId) + resp, err := s.sendRequest("POST", url, nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted) +} + +func (s *MantaHTTPSuite) TestGetJob(c *gc.C) { + var expected manta.Job + jobId := s.createJob(c, "test-job") + + url := fmt.Sprintf("%s/%s/live/status", fakeJobsPrefix, jobId) + resp, err := s.sendRequest("GET", url, nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(expected.Id, gc.Equals, jobId) + c.Assert(expected.Name, gc.Equals, "test-job") + c.Assert(expected.Cancelled, gc.Equals, false) + c.Assert(expected.InputDone, gc.Equals, false) + c.Assert(expected.Phases, gc.HasLen, 2) +} + +func (s *MantaHTTPSuite) TestGetJobInput(c *gc.C) { + var expected string + jobId := s.createJob(c, "test-job") + + url := fmt.Sprintf("%s/%s/live/in", fakeJobsPrefix, jobId) + resp, err := s.sendRequest("GET", url, nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + fmt.Println(expected) + c.Assert(expected, gc.HasLen, 0) +} + +func (s *MantaHTTPSuite) TestGetJobOutput(c *gc.C) { + var expected string + jobId := s.createJob(c, "test-job") + + url := fmt.Sprintf("%s/%s/live/out", fakeJobsPrefix, jobId) + resp, err := s.sendRequest("GET", url, nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(strings.Split(expected, "\n"), gc.HasLen, 1) +} + +func (s *MantaHTTPSuite) TestGetJobFailures(c *gc.C) { + var expected string + jobId := s.createJob(c, "test-job") + + url := fmt.Sprintf("%s/%s/live/fail", fakeJobsPrefix, jobId) + resp, err := s.sendRequest("GET", url, nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(expected, gc.Equals, "") +} + +func (s *MantaHTTPSuite) TestGetJobErrors(c *gc.C) { + var expected []manta.JobError + jobId := s.createJob(c, "test-job") + + url := fmt.Sprintf("%s/%s/live/err", fakeJobsPrefix, jobId) + resp, err := s.sendRequest("GET", url, nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(expected, gc.HasLen, 0) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/manta/service_test.go juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/manta/service_test.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/localservices/manta/service_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/localservices/manta/service_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,358 @@ +// +// gomanta - Go library to interact with Joyent Manta +// +// Manta double testing service - internal direct API test +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package manta_test + +import ( + "fmt" + gc "launchpad.net/gocheck" + "strings" + "testing" + + "encoding/json" + + lm "github.com/joyent/gomanta/localservices/manta" + "github.com/joyent/gomanta/manta" +) + +type MantaSuite struct { + service *lm.Manta +} + +const ( + testServiceURL = "https://go-test.manta.joyent.com" + testUserAccount = "gouser" + object = "1. Go Test -- Go Test -- GoTest\n2. Go Test -- Go Test -- GoTest" +) + +var _ = gc.Suite(&MantaSuite{}) + +func Test(t *testing.T) { + gc.TestingT(t) +} + +func (s *MantaSuite) SetUpSuite(c *gc.C) { + s.service = lm.New(testServiceURL, testUserAccount) +} + +// Helpers +func getObject() ([]byte, error) { + var bytes []byte + + r := strings.NewReader(object) + if _, err := r.Read(bytes); err != nil { + return nil, err + } + + return bytes, nil +} + +func (s *MantaSuite) createDirectory(c *gc.C, path string) { + err := s.service.PutDirectory(path) + c.Assert(err, gc.IsNil) +} + +func (s *MantaSuite) deleteDirectory(c *gc.C, path string) { + err := s.service.DeleteDirectory(path) + c.Assert(err, gc.IsNil) +} + +func (s *MantaSuite) createObject(c *gc.C, path, objName string) { + obj, err := getObject() + c.Assert(err, gc.IsNil) + err = s.service.PutObject(path, objName, obj) + c.Assert(err, gc.IsNil) +} + +func (s *MantaSuite) deleteObject(c *gc.C, path string) { + err := s.service.DeleteObject(path) + c.Assert(err, gc.IsNil) +} + +func createTestJob(c *gc.C, jobName string) []byte { + phases := []manta.Phase{{Type: "map", Exec: "wc", Init: ""}, {Type: "reduce", Exec: "awk '{ l += $1; w += $2; c += $3 } END { print l, w, c }'", Init: ""}} + job, err := json.Marshal(manta.CreateJobOpts{Name: jobName, Phases: phases}) + c.Assert(err, gc.IsNil) + return job +} + +func (s *MantaSuite) createJob(c *gc.C, jobName string) string { + jobUri, err := s.service.CreateJob(createTestJob(c, jobName)) + c.Assert(err, gc.IsNil) + c.Assert(jobUri, gc.NotNil) + return strings.Split(jobUri, "/")[3] +} + +// Storage APIs +func (s *MantaSuite) TestPutDirectory(c *gc.C) { + s.createDirectory(c, "test") +} + +func (s *MantaSuite) TestPutDirectoryWithParent(c *gc.C) { + s.createDirectory(c, "test") + s.createDirectory(c, "test/innerdir") +} + +func (s *MantaSuite) TestPutDirectoryNoParent(c *gc.C) { + err := s.service.PutDirectory("nodir/test") + c.Assert(err, gc.ErrorMatches, "/gouser/stor/nodir was not found") +} + +func (s *MantaSuite) TestListDirectoryEmpty(c *gc.C) { + s.createDirectory(c, "empty") + dirs, err := s.service.ListDirectory("empty", "", 0) + c.Assert(err, gc.IsNil) + c.Assert(dirs, gc.HasLen, 0) +} + +func (s *MantaSuite) TestListDirectoryNoExists(c *gc.C) { + _, err := s.service.ListDirectory("nodir", "", 0) + c.Assert(err, gc.ErrorMatches, "/gouser/stor/nodir was not found") +} + +func (s *MantaSuite) TestListDirectory(c *gc.C) { + s.createDirectory(c, "dir") + for i := 0; i < 5; i++ { + s.createObject(c, "dir", fmt.Sprintf("obj%d", i)) + } + dirs, err := s.service.ListDirectory("dir", "", 0) + c.Assert(err, gc.IsNil) + c.Assert(dirs, gc.HasLen, 5) +} + +func (s *MantaSuite) TestListDirectoryWithLimit(c *gc.C) { + s.createDirectory(c, "limitdir") + for i := 0; i < 500; i++ { + s.createObject(c, "limitdir", fmt.Sprintf("obj%03d", i)) + } + dirs, err := s.service.ListDirectory("limitdir", "", 0) + c.Assert(err, gc.IsNil) + c.Assert(dirs, gc.HasLen, 256) + dirs, err = s.service.ListDirectory("limitdir", "", 10) + c.Assert(err, gc.IsNil) + c.Assert(dirs, gc.HasLen, 10) + dirs, err = s.service.ListDirectory("limitdir", "", 300) + c.Assert(err, gc.IsNil) + c.Assert(dirs, gc.HasLen, 300) +} + +func (s *MantaSuite) TestListDirectoryWithMarker(c *gc.C) { + s.createDirectory(c, "markerdir") + for i := 0; i < 500; i++ { + s.createObject(c, "markerdir", fmt.Sprintf("obj%03d", i)) + } + dirs, err := s.service.ListDirectory("markerdir", "obj400", 0) + c.Assert(err, gc.IsNil) + c.Assert(dirs, gc.HasLen, 100) + c.Assert(dirs[0].Name, gc.Equals, "obj400") + +} + +func (s *MantaSuite) TestDeleteDirectoryNotEmpty(c *gc.C) { + s.createDirectory(c, "notempty") + s.createObject(c, "notempty", "obj") + err := s.service.DeleteDirectory("notempty") + c.Assert(err, gc.ErrorMatches, "BadRequestError") +} + +func (s *MantaSuite) TestDeleteDirectory(c *gc.C) { + s.createDirectory(c, "deletedir") + s.deleteDirectory(c, "deletedir") +} + +func (s *MantaSuite) TestDeleteDirectoryNoExists(c *gc.C) { + s.deleteDirectory(c, "nodir") +} + +func (s *MantaSuite) TestPutObject(c *gc.C) { + s.createDirectory(c, "objdir") + s.createObject(c, "objdir", "object") +} + +func (s *MantaSuite) TestPutObjectDirectoryWithParent(c *gc.C) { + s.createDirectory(c, "parent") + s.createDirectory(c, "parent/objdir") + s.createObject(c, "parent/objdir", "object") +} + +func (s *MantaSuite) TestPutObjectDirectoryNoParent(c *gc.C) { + obj, err := getObject() + c.Assert(err, gc.IsNil) + err = s.service.PutObject("nodir", "obj", obj) + c.Assert(err, gc.ErrorMatches, "/gouser/stor/nodir was not found") +} + +func (s *MantaSuite) TestIsObjectTrue(c *gc.C) { + s.createDirectory(c, "objdir") + s.createObject(c, "objdir", "obj") + isObj := s.service.IsObject("/gouser/stor/objdir/obj") + c.Assert(isObj, gc.Equals, true) +} + +func (s *MantaSuite) TestIsObjectFalse(c *gc.C) { + isObj := s.service.IsObject("/gouser/stor/nodir/obj") + c.Assert(isObj, gc.Equals, false) +} + +func (s *MantaSuite) TestGetObject(c *gc.C) { + s.createDirectory(c, "dir1") + s.createObject(c, "dir1", "obj") + expected, _ := getObject() + obj, err := s.service.GetObject("dir1/obj") + c.Assert(err, gc.IsNil) + c.Assert(obj, gc.DeepEquals, expected) +} + +func (s *MantaSuite) TestGetObjectWrongPath(c *gc.C) { + obj, err := s.service.GetObject("nodir/obj") + c.Assert(err, gc.ErrorMatches, "/gouser/stor/nodir/obj was not found") + c.Assert(obj, gc.IsNil) +} + +func (s *MantaSuite) TestGetObjectWrongName(c *gc.C) { + obj, err := s.service.GetObject("noobject") + c.Assert(err, gc.ErrorMatches, "/gouser/stor/noobject was not found") + c.Assert(obj, gc.IsNil) +} + +func (s *MantaSuite) TestDeleteObject(c *gc.C) { + s.createDirectory(c, "delete") + s.createObject(c, "delete", "obj") + s.deleteObject(c, "delete/obj") +} + +func (s *MantaSuite) TestDeleteObjectWrongPath(c *gc.C) { + err := s.service.DeleteObject("nodir/obj") + c.Assert(err, gc.ErrorMatches, "/gouser/stor/nodir/obj was not found") +} + +func (s *MantaSuite) TestDeleteObjectWrongName(c *gc.C) { + err := s.service.DeleteObject("noobj") + c.Assert(err, gc.ErrorMatches, "/gouser/stor/noobj was not found") +} + +func (s *MantaSuite) TestPutSnapLink(c *gc.C) { + s.createDirectory(c, "linkdir") + s.createObject(c, "linkdir", "obj") + err := s.service.PutSnapLink("linkdir", "link", "/gouser/stor/linkdir/obj") + c.Assert(err, gc.IsNil) +} + +func (s *MantaSuite) TestPutSnapLinkNoLocation(c *gc.C) { + s.createDirectory(c, "linkdir") + s.createObject(c, "linkdir", "obj") + err := s.service.PutSnapLink("linkdir", "link", "/gouser/stor/linkdir/noobj") + c.Assert(err, gc.ErrorMatches, "/gouser/stor/linkdir/noobj was not found") +} + +func (s *MantaSuite) TestPutSnapLinkDirectoryWithParent(c *gc.C) { + s.createDirectory(c, "link1") + s.createDirectory(c, "link1/linkdir") + s.createObject(c, "link1/linkdir", "obj") + err := s.service.PutSnapLink("link1/linkdir", "link", "/gouser/stor/link1/linkdir/obj") + c.Assert(err, gc.IsNil) +} + +func (s *MantaSuite) TestPutSnapLinkDirectoryNoParent(c *gc.C) { + s.createDirectory(c, "linkdir") + s.createObject(c, "linkdir", "obj") + err := s.service.PutSnapLink("nodir", "link", "/gouser/stor/linkdir/obj") + c.Assert(err, gc.ErrorMatches, "/gouser/stor/nodir was not found") +} + +// Jobs API +func (s *MantaSuite) TestCreateJob(c *gc.C) { + s.createJob(c, "test-job") +} + +func (s *MantaSuite) TestListAllJobs(c *gc.C) { + s.createJob(c, "test-job") + jobs, err := s.service.ListJobs(false) + c.Assert(err, gc.IsNil) + c.Assert(jobs, gc.NotNil) +} + +func (s *MantaSuite) TestListLiveJobs(c *gc.C) { + s.createJob(c, "test-job") + jobs, err := s.service.ListJobs(true) + c.Assert(err, gc.IsNil) + c.Assert(jobs, gc.NotNil) + //c.Assert(jobs, gc.Equals, 1) +} + +func (s *MantaSuite) TestCancelJob(c *gc.C) { + jobId := s.createJob(c, "test-job") + err := s.service.CancelJob(jobId) + c.Assert(err, gc.IsNil) +} + +func (s *MantaSuite) TestJ05_AddJobInputs(c *gc.C) { + var inBytes []byte + var inputs = `/` + testUserAccount + `/stor/testjob/obj1 +/` + testUserAccount + `/stor/testjob/obj2 +` + r := strings.NewReader(inputs) + if _, err := r.Read(inBytes); err != nil { + c.Skip("Could not read input, skipping...") + } + + s.createDirectory(c, "testjob") + s.createObject(c, "testjob", "obj1") + s.createObject(c, "testjob", "obj2") + + jobId := s.createJob(c, "test-job") + err := s.service.AddJobInputs(jobId, inBytes) + c.Assert(err, gc.IsNil) +} + +func (s *MantaSuite) TestJ06_EndJobInputs(c *gc.C) { + jobId := s.createJob(c, "test-job") + err := s.service.EndJobInput(jobId) + c.Assert(err, gc.IsNil) +} + +func (s *MantaSuite) TestJ07_GetJob(c *gc.C) { + jobId := s.createJob(c, "test-job") + job, err := s.service.GetJob(jobId) + c.Assert(err, gc.IsNil) + c.Assert(job, gc.NotNil) +} + +func (s *MantaSuite) TestJ08_GetJobInput(c *gc.C) { + jobId := s.createJob(c, "test-job") + input, err := s.service.GetJobInput(jobId) + c.Assert(err, gc.IsNil) + c.Assert(input, gc.NotNil) + //fmt.Println(input) +} + +func (s *MantaSuite) TestJ09_GetJobOutput(c *gc.C) { + jobId := s.createJob(c, "test-job") + output, err := s.service.GetJobOutput(jobId) + c.Assert(err, gc.IsNil) + c.Assert(output, gc.NotNil) + //fmt.Println(output) +} + +func (s *MantaSuite) TestJ10_GetJobFailures(c *gc.C) { + jobId := s.createJob(c, "test-job") + fail, err := s.service.GetJobFailures(jobId) + c.Assert(err, gc.IsNil) + c.Assert(fail, gc.Equals, "") + //fmt.Println(fail) +} + +func (s *MantaSuite) TestJ11_GetJobErrors(c *gc.C) { + jobId := s.createJob(c, "test-job") + errs, err := s.service.GetJobErrors(jobId) + c.Assert(err, gc.IsNil) + c.Assert(errs, gc.IsNil) + //fmt.Println(errs) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/manta/live_test.go juju-core-1.18.0/src/github.com/joyent/gomanta/manta/live_test.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/manta/live_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/manta/live_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,249 @@ +// +// gomanta - Go library to interact with Joyent Manta +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package manta_test + +import ( + "fmt" + "net/http" + "time" + + "github.com/joyent/gocommon/client" + "github.com/joyent/gomanta/manta" + "github.com/joyent/gosign/auth" + gc "launchpad.net/gocheck" + "strings" +) + +func registerMantaTests(creds *auth.Credentials) { + gc.Suite(&LiveTests{creds: creds}) +} + +type LiveTests struct { + creds *auth.Credentials + testClient *manta.Client +} + +func (s *LiveTests) SetUpTest(c *gc.C) { + client := client.NewClient(s.creds.MantaEndpoint.URL, "", s.creds, &manta.Logger) + c.Assert(client, gc.NotNil) + s.testClient = manta.New(client) + c.Assert(s.testClient, gc.NotNil) +} + +// Helper method to create a test directory +func (s *LiveTests) createDirectory(c *gc.C, path string) { + err := s.testClient.PutDirectory(path) + c.Assert(err, gc.IsNil) +} + +// Helper method to create a test object +func (s *LiveTests) createObject(c *gc.C, path, objName string) { + err := s.testClient.PutObject(path, objName, []byte("Test Manta API")) + c.Assert(err, gc.IsNil) +} + +// Helper method to delete a test directory +func (s *LiveTests) deleteDirectory(c *gc.C, path string) { + err := s.testClient.DeleteDirectory(path) + c.Assert(err, gc.IsNil) +} + +// Helper method to delete a test object +func (s *LiveTests) deleteObject(c *gc.C, path, objName string) { + err := s.testClient.DeleteObject(path, objName) + c.Assert(err, gc.IsNil) +} + +// Helper method to create a test job +func (s *LiveTests) createJob(c *gc.C, jobName string) string { + phases := []manta.Phase{ + {Type: "map", Exec: "wc", Init: ""}, + {Type: "reduce", Exec: "awk '{ l += $1; w += $2; c += $3 } END { print l, w, c }'", Init: ""}, + } + jobUri, err := s.testClient.CreateJob(manta.CreateJobOpts{Name: jobName, Phases: phases}) + c.Assert(err, gc.IsNil) + c.Assert(jobUri, gc.NotNil) + return strings.Split(jobUri, "/")[3] +} + +// Storage API +func (s *LiveTests) TestPutDirectory(c *gc.C) { + s.createDirectory(c, "test") + + // cleanup + s.deleteDirectory(c, "test") +} + +func (s *LiveTests) TestListDirectory(c *gc.C) { + s.createDirectory(c, "test") + defer s.deleteDirectory(c, "test") + s.createObject(c, "test", "obj") + defer s.deleteObject(c, "test", "obj") + + opts := manta.ListDirectoryOpts{} + dirs, err := s.testClient.ListDirectory("test", opts) + c.Assert(err, gc.IsNil) + c.Assert(dirs, gc.NotNil) +} + +func (s *LiveTests) TestDeleteDirectory(c *gc.C) { + s.createDirectory(c, "test") + + s.deleteDirectory(c, "test") +} + +func (s *LiveTests) TestPutObject(c *gc.C) { + s.createDirectory(c, "dir") + defer s.deleteDirectory(c, "dir") + s.createObject(c, "dir", "obj") + defer s.deleteObject(c, "dir", "obj") +} + +func (s *LiveTests) TestGetObject(c *gc.C) { + s.createDirectory(c, "dir") + defer s.deleteDirectory(c, "dir") + s.createObject(c, "dir", "obj") + defer s.deleteObject(c, "dir", "obj") + + obj, err := s.testClient.GetObject("dir", "obj") + c.Assert(err, gc.IsNil) + c.Assert(obj, gc.NotNil) + c.Check(string(obj), gc.Equals, "Test Manta API") +} + +func (s *LiveTests) TestDeleteObject(c *gc.C) { + s.createDirectory(c, "dir") + defer s.deleteDirectory(c, "dir") + s.createObject(c, "dir", "obj") + + s.deleteObject(c, "dir", "obj") +} + +func (s *LiveTests) TestPutSnapLink(c *gc.C) { + s.createDirectory(c, "linkdir") + defer s.deleteDirectory(c, "linkdir") + s.createObject(c, "linkdir", "obj") + defer s.deleteObject(c, "linkdir", "obj") + + location := fmt.Sprintf("%s/%s", s.creds.UserAuthentication.User, "stor/linkdir/obj") + err := s.testClient.PutSnapLink("linkdir", "objlnk", location) + c.Assert(err, gc.IsNil) + + // cleanup + s.deleteObject(c, "linkdir", "objlnk") +} + +func (s *LiveTests) TestSignURL(c *gc.C) { + s.createDirectory(c, "sign") + defer s.deleteDirectory(c, "sign") + s.createObject(c, "sign", "object") + defer s.deleteObject(c, "sign", "object") + + location := fmt.Sprintf("/%s/%s", s.creds.UserAuthentication.User, "stor/sign/object") + url, err := s.testClient.SignURL(location, time.Now().Add(time.Minute*5)) + c.Assert(err, gc.IsNil) + c.Assert(url, gc.Not(gc.Equals), "") + + resp, err := http.Get(url) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) +} + +// Jobs API +func (s *LiveTests) TestCreateJob(c *gc.C) { + s.createJob(c, "test-job") +} + +func (s *LiveTests) TestListLiveJobs(c *gc.C) { + s.createJob(c, "test-job") + + jobs, err := s.testClient.ListJobs(true) + c.Assert(err, gc.IsNil) + c.Assert(jobs, gc.NotNil) + c.Assert(len(jobs) >= 1, gc.Equals, true) +} + +func (s *LiveTests) TestListAllJobs(c *gc.C) { + s.createJob(c, "test-job") + + jobs, err := s.testClient.ListJobs(false) + c.Assert(err, gc.IsNil) + c.Assert(jobs, gc.NotNil) +} + +func (s *LiveTests) TestCancelJob(c *gc.C) { + jobId := s.createJob(c, "test-job") + + err := s.testClient.CancelJob(jobId) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestAddJobInputs(c *gc.C) { + var inputs = `/` + s.creds.UserAuthentication.User + `/stor/testjob/obj1 +/` + s.creds.UserAuthentication.User + `/stor/testjob/obj2 +` + s.createDirectory(c, "testjob") + defer s.deleteDirectory(c, "testjob") + s.createObject(c, "testjob", "obj1") + defer s.deleteObject(c, "testjob", "obj1") + s.createObject(c, "testjob", "obj2") + defer s.deleteObject(c, "testjob", "obj2") + jobId := s.createJob(c, "test-job") + + err := s.testClient.AddJobInputs(jobId, strings.NewReader(inputs)) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestEndJobInputs(c *gc.C) { + jobId := s.createJob(c, "test-job") + + err := s.testClient.EndJobInputs(jobId) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestGetJob(c *gc.C) { + jobId := s.createJob(c, "test-job") + + job, err := s.testClient.GetJob(jobId) + c.Assert(err, gc.IsNil) + c.Assert(job, gc.NotNil) +} + +func (s *LiveTests) TestGetJobInput(c *gc.C) { + jobId := s.createJob(c, "test-job") + + input, err := s.testClient.GetJobInput(jobId) + c.Assert(err, gc.IsNil) + c.Assert(input, gc.NotNil) +} + +func (s *LiveTests) TestGetJobOutput(c *gc.C) { + jobId := s.createJob(c, "test-job") + + output, err := s.testClient.GetJobOutput(jobId) + c.Assert(err, gc.IsNil) + c.Assert(output, gc.NotNil) +} + +func (s *LiveTests) TestGetJobFailures(c *gc.C) { + jobId := s.createJob(c, "test-job") + + fail, err := s.testClient.GetJobFailures(jobId) + c.Assert(err, gc.IsNil) + c.Assert(fail, gc.Equals, nil) +} + +func (s *LiveTests) TestGetJobErrors(c *gc.C) { + jobId := s.createJob(c, "test-job") + + errs, err := s.testClient.GetJobErrors(jobId) + c.Assert(err, gc.IsNil) + c.Assert(errs, gc.IsNil) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/manta/local_test.go juju-core-1.18.0/src/github.com/joyent/gomanta/manta/local_test.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/manta/local_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/manta/local_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,275 @@ +// +// gomanta - Go library to interact with Joyent Manta +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package manta_test + +import ( + "fmt" + "io/ioutil" + "net/http" + "net/http/httptest" + "os" + "strings" + + gc "launchpad.net/gocheck" + + "github.com/joyent/gocommon/client" + localmanta "github.com/joyent/gomanta/localservices/manta" + "github.com/joyent/gomanta/manta" + "github.com/joyent/gosign/auth" +) + +var privateKey []byte + +func registerLocalTests(keyName string) { + var localKeyFile string + if keyName == "" { + localKeyFile = os.Getenv("HOME") + "/.ssh/id_rsa" + } else { + localKeyFile = keyName + } + privateKey, _ = ioutil.ReadFile(localKeyFile) + + gc.Suite(&LocalTests{}) +} + +type LocalTests struct { + LiveTests + Server *httptest.Server + Mux *http.ServeMux + oldHandler http.Handler + manta *localmanta.Manta +} + +func (s *LocalTests) SetUpSuite(c *gc.C) { + // Set up the HTTP server. + s.Server = httptest.NewServer(nil) + s.oldHandler = s.Server.Config.Handler + s.Mux = http.NewServeMux() + s.Server.Config.Handler = s.Mux + + // Set up a Joyent Manta service. + authentication := auth.Auth{User: "localtest", PrivateKey: string(privateKey), Algorithm: "rsa-sha256"} + + s.creds = &auth.Credentials{ + UserAuthentication: authentication, + MantaKeyId: "", + MantaEndpoint: auth.Endpoint{URL: s.Server.URL}, + } + s.manta = localmanta.New(s.creds.MantaEndpoint.URL, s.creds.UserAuthentication.User) + s.manta.SetupHTTP(s.Mux) +} + +func (s *LocalTests) TearDownSuite(c *gc.C) { + s.Mux = nil + s.Server.Config.Handler = s.oldHandler + s.Server.Close() +} + +func (s *LocalTests) SetUpTest(c *gc.C) { + client := client.NewClient(s.creds.MantaEndpoint.URL, "", s.creds, &manta.Logger) + c.Assert(client, gc.NotNil) + s.testClient = manta.New(client) + c.Assert(s.testClient, gc.NotNil) +} + +// Helper method to create a test directory +func (s *LocalTests) createDirectory(c *gc.C, path string) { + err := s.testClient.PutDirectory(path) + c.Assert(err, gc.IsNil) +} + +// Helper method to create a test object +func (s *LocalTests) createObject(c *gc.C, path, objName string) { + err := s.testClient.PutObject(path, objName, []byte("Test Manta API")) + c.Assert(err, gc.IsNil) +} + +// Helper method to delete a test directory +func (s *LocalTests) deleteDirectory(c *gc.C, path string) { + err := s.testClient.DeleteDirectory(path) + c.Assert(err, gc.IsNil) +} + +// Helper method to delete a test object +func (s *LocalTests) deleteObject(c *gc.C, path, objName string) { + err := s.testClient.DeleteObject(path, objName) + c.Assert(err, gc.IsNil) +} + +// Helper method to create a test job +func (s *LocalTests) createJob(c *gc.C, jobName string) string { + phases := []manta.Phase{ + {Type: "map", Exec: "wc", Init: ""}, + {Type: "reduce", Exec: "awk '{ l += $1; w += $2; c += $3 } END { print l, w, c }'", Init: ""}, + } + jobUri, err := s.testClient.CreateJob(manta.CreateJobOpts{Name: jobName, Phases: phases}) + c.Assert(err, gc.IsNil) + c.Assert(jobUri, gc.NotNil) + return strings.Split(jobUri, "/")[3] +} + +// Storage API +func (s *LocalTests) TestPutDirectory(c *gc.C) { + s.createDirectory(c, "test") + + // cleanup + s.deleteDirectory(c, "test") +} + +func (s *LocalTests) TestListDirectory(c *gc.C) { + s.createDirectory(c, "test") + defer s.deleteDirectory(c, "test") + s.createObject(c, "test", "obj") + defer s.deleteObject(c, "test", "obj") + + opts := manta.ListDirectoryOpts{} + dirs, err := s.testClient.ListDirectory("test", opts) + c.Assert(err, gc.IsNil) + c.Assert(dirs, gc.NotNil) +} + +func (s *LocalTests) TestDeleteDirectory(c *gc.C) { + s.createDirectory(c, "test") + + s.deleteDirectory(c, "test") +} + +func (s *LocalTests) TestPutObject(c *gc.C) { + s.createDirectory(c, "dir") + defer s.deleteDirectory(c, "dir") + s.createObject(c, "dir", "obj") + defer s.deleteObject(c, "dir", "obj") +} + +func (s *LocalTests) TestGetObject(c *gc.C) { + s.createDirectory(c, "dir") + defer s.deleteDirectory(c, "dir") + s.createObject(c, "dir", "obj") + defer s.deleteObject(c, "dir", "obj") + + obj, err := s.testClient.GetObject("dir", "obj") + c.Assert(err, gc.IsNil) + c.Assert(obj, gc.NotNil) + c.Check(string(obj), gc.Equals, "Test Manta API") +} + +func (s *LocalTests) TestDeleteObject(c *gc.C) { + s.createDirectory(c, "dir") + defer s.deleteDirectory(c, "dir") + s.createObject(c, "dir", "obj") + + s.deleteObject(c, "dir", "obj") +} + +func (s *LocalTests) TestPutSnapLink(c *gc.C) { + s.createDirectory(c, "linkdir") + defer s.deleteDirectory(c, "linkdir") + s.createObject(c, "linkdir", "obj") + defer s.deleteObject(c, "linkdir", "obj") + + location := fmt.Sprintf("/%s/%s", s.creds.UserAuthentication.User, "stor/linkdir/obj") + err := s.testClient.PutSnapLink("linkdir", "objlnk", location) + c.Assert(err, gc.IsNil) + + // cleanup + s.deleteObject(c, "linkdir", "objlnk") +} + +// Jobs API +func (s *LocalTests) TestCreateJob(c *gc.C) { + s.createJob(c, "test-job") +} + +func (s *LocalTests) TestListLiveJobs(c *gc.C) { + s.createJob(c, "test-job") + + jobs, err := s.testClient.ListJobs(true) + c.Assert(err, gc.IsNil) + c.Assert(jobs, gc.NotNil) + c.Assert(len(jobs) >= 1, gc.Equals, true) +} + +func (s *LocalTests) TestListAllJobs(c *gc.C) { + s.createJob(c, "test-job") + + jobs, err := s.testClient.ListJobs(false) + c.Assert(err, gc.IsNil) + c.Assert(jobs, gc.NotNil) +} + +func (s *LocalTests) TestCancelJob(c *gc.C) { + jobId := s.createJob(c, "test-job") + + err := s.testClient.CancelJob(jobId) + c.Assert(err, gc.IsNil) +} + +func (s *LocalTests) TestAddJobInputs(c *gc.C) { + var inputs = `/` + s.creds.UserAuthentication.User + `/stor/testjob/obj1 +/` + s.creds.UserAuthentication.User + `/stor/testjob/obj2 +` + s.createDirectory(c, "testjob") + defer s.deleteDirectory(c, "testjob") + s.createObject(c, "testjob", "obj1") + defer s.deleteObject(c, "testjob", "obj1") + s.createObject(c, "testjob", "obj2") + defer s.deleteObject(c, "testjob", "obj2") + jobId := s.createJob(c, "test-job") + + err := s.testClient.AddJobInputs(jobId, strings.NewReader(inputs)) + c.Assert(err, gc.IsNil) +} + +func (s *LocalTests) TestEndJobInputs(c *gc.C) { + jobId := s.createJob(c, "test-job") + + err := s.testClient.EndJobInputs(jobId) + c.Assert(err, gc.IsNil) +} + +func (s *LocalTests) TestGetJob(c *gc.C) { + jobId := s.createJob(c, "test-job") + + job, err := s.testClient.GetJob(jobId) + c.Assert(err, gc.IsNil) + c.Assert(job, gc.NotNil) +} + +func (s *LocalTests) TestGetJobInput(c *gc.C) { + jobId := s.createJob(c, "test-job") + + input, err := s.testClient.GetJobInput(jobId) + c.Assert(err, gc.IsNil) + c.Assert(input, gc.NotNil) +} + +func (s *LocalTests) TestGetJobOutput(c *gc.C) { + jobId := s.createJob(c, "test-job") + + output, err := s.testClient.GetJobOutput(jobId) + c.Assert(err, gc.IsNil) + c.Assert(output, gc.NotNil) +} + +func (s *LocalTests) TestGetJobFailures(c *gc.C) { + jobId := s.createJob(c, "test-job") + + fail, err := s.testClient.GetJobFailures(jobId) + c.Assert(err, gc.IsNil) + c.Assert(fail, gc.Equals, "") +} + +func (s *LocalTests) TestGetJobErrors(c *gc.C) { + jobId := s.createJob(c, "test-job") + + errs, err := s.testClient.GetJobErrors(jobId) + c.Assert(err, gc.IsNil) + c.Assert(errs, gc.IsNil) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/manta/manta.go juju-core-1.18.0/src/github.com/joyent/gomanta/manta/manta.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/manta/manta.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/manta/manta.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,459 @@ +/* +The gomanta/manta package interacts with the Manta API (http://apidocs.joyent.com/manta/api.html). + +Licensed under LGPL v3. + +Copyright (c) 2013 Joyent Inc. +Written by Daniele Stroppa + +*/ +package manta + +import ( + "bytes" + "fmt" + "io" + "io/ioutil" + "net/http" + "path" + "time" + + "github.com/juju/loggo" + + "github.com/joyent/gocommon/client" + "github.com/joyent/gocommon/errors" + + jh "github.com/joyent/gocommon/http" +) + +// Logger for this package +var Logger = loggo.GetLogger("gomanta.manta") + +const ( + // The default version of the Manta API to use + DefaultAPIVersion = "7.1" + + // Manta API URL parts + apiStorage = "stor" + apiJobs = "jobs" + apiJobsLive = "live" + apiJobsIn = "in" + apiJobsOut = "out" + apiJobsFail = "fail" + apiJobsErr = "err" + apiJobsEnd = "end" + apiJobsCancel = "cancel" + apiJobsStatus = "status" +) + +// Client provides a means to access Joyent Manta +type Client struct { + client client.Client +} + +// New creates a new Client. +func New(client client.Client) *Client { + return &Client{client} +} + +// request represents an API request +type request struct { + method string + url string + reqValue interface{} + reqHeader http.Header + reqReader io.Reader + reqLength int + resp interface{} + respHeader *http.Header + expectedStatus int +} + +// Helper method to send an API request +func (c *Client) sendRequest(req request) (*jh.ResponseData, error) { + request := jh.RequestData{ + ReqValue: req.reqValue, + ReqHeaders: req.reqHeader, + ReqReader: req.reqReader, + ReqLength: req.reqLength, + } + if req.expectedStatus == 0 { + req.expectedStatus = http.StatusOK + } + respData := jh.ResponseData{ + RespValue: req.resp, + RespHeaders: req.respHeader, + ExpectedStatus: []int{req.expectedStatus}, + } + err := c.client.SendRequest(req.method, req.url, "", &request, &respData) + return &respData, err +} + +// Helper method to create the API URL +func makeURL(parts ...string) string { + return path.Join(parts...) +} + +// ListDirectoryOpts represent the option that can be specified +// when listing a directory. +type ListDirectoryOpts struct { + Limit int `json:"limit"` // Limit to the number of records returned (default and max is 1000) + Marker string `json:"marker"` // Key name at which to start the next listing +} + +// Entry represents an object stored in Manta, either a file or a directory +type Entry struct { + Name string `json:"name"` // Entry name + Etag string `json:"etag,omitempty"` // If type is 'object', object UUID + Size int `json:"size,omitempty"` // If type is 'object', object size (content-length) + Type string `json:"type"` // Entry type, one of 'directory' or 'object' + Mtime string `json:"mtime"` // ISO8601 timestamp of the last update +} + +// Creates a directory at the specified path. Any parent directory must exist. +// See API docs: http://apidocs.joyent.com/manta/api.html#PutDirectory +func (c *Client) PutDirectory(path string) error { + requestHeaders := make(http.Header) + requestHeaders.Set("Content-Type", "application/json; type=directory") + requestHeaders.Set("Accept", "*/*") + req := request{ + method: client.PUT, + url: makeURL(apiStorage, path), + reqHeader: requestHeaders, + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to create directory: %s", path) + } + return nil +} + +// Returns the content of the specified directory, using the specified options. +// See API docs: http://apidocs.joyent.com/manta/api.html#ListDirectory +func (c *Client) ListDirectory(directory string, opts ListDirectoryOpts) ([]Entry, error) { + var resp []Entry + requestHeaders := make(http.Header) + requestHeaders.Set("Accept", "*/*") + req := request{ + method: client.GET, + url: makeURL(apiStorage, directory), + reqHeader: requestHeaders, + resp: &resp, + reqValue: opts, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to list directory %s", directory) + } + return resp, nil +} + +// Deletes the specified directory. Directory must be empty. +// See API docs: http://apidocs.joyent.com/manta/api.html#DeleteDirectory +func (c *Client) DeleteDirectory(path string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiStorage, path), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete directory %s", path) + } + return nil +} + +// Creates an object at the specified path. Any parent directory must exist. +// See API docs: http://apidocs.joyent.com/manta/api.html#PutObject +func (c *Client) PutObject(path, objectName string, object []byte) error { + r := bytes.NewReader(object) + req := request{ + method: client.PUT, + url: makeURL(apiStorage, path, objectName), + reqReader: r, + reqLength: len(object), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to create object: %s/%s", path, objectName) + } + return nil +} + +// Retrieves the specified object from the specified location. +// See API docs: http://apidocs.joyent.com/manta/api.html#GetObject +func (c *Client) GetObject(path, objectName string) ([]byte, error) { + var resp []byte + requestHeaders := make(http.Header) + requestHeaders.Set("Accept", "*/*") + req := request{ + method: client.GET, + url: makeURL(apiStorage, path, objectName), + reqHeader: requestHeaders, + resp: &resp, + } + respData, err := c.sendRequest(req) + if err != nil { + return nil, errors.Newf(err, "failed to get object %s/%s", path, objectName) + } + res, _ := respData.RespValue.([]byte) + return res, nil +} + +// Deletes the specified object from the specified location. +// See API docs: http://apidocs.joyent.com/manta/api.html#DeleteObject +func (c *Client) DeleteObject(path, objectName string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiStorage, path, objectName), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete object %s/%s", path, objectName) + } + return nil +} + +// Creates a link (similar to a Unix hard link) from location to path/linkName. +// See API docs: http://apidocs.joyent.com/manta/api.html#PutSnapLink +func (c *Client) PutSnapLink(path, linkName, location string) error { + requestHeaders := make(http.Header) + requestHeaders.Set("Accept", "application/json; type=link") + requestHeaders.Set("Location", location) + req := request{ + method: client.PUT, + url: makeURL(apiStorage, path, linkName), + reqHeader: requestHeaders, + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to create snap link: %s/%s", path, linkName) + } + return nil +} + +// CreateJobOpts represent the option that can be specified +// when creating a job. +type CreateJobOpts struct { + Name string `json:"name,omitempty"` // Job Name (optional) + Phases []Phase `json:"phases"` // Tasks to execute as part of this job +} + +// Job represents the status of a job. +type Job struct { + Id string // Job unique identifier + Name string `json:"name,omitempty"` // Job Name + State string // Job state + Cancelled bool // Whether the job has been cancelled or not + InputDone bool // Whether the inputs for the job is still open or not + Stats JobStats `json:"stats,omitempty"` // Job statistics + TimeCreated string // Time the job was created at + TimeDone string `json:"timeDone,omitempty"` // Time the job was completed + TimeArchiveStarted string `json:"timeArchiveStarted,omitempty"` // Time the job archiving started + TimeArchiveDone string `json:"timeArchiveDone,omitempty"` // Time the job archiving completed + Phases []Phase `json:"phases"` // Job tasks + Options interface{} // Job options +} + +// JobStats represents statistics about a job +type JobStats struct { + Errors int // Number or errors + Outputs int // Number of output produced + Retries int // Number of retries + Tasks int // Total number of task in the job + TasksDone int // number of tasks done +} + +// Phase represents a task to be executed as part of a Job +type Phase struct { + Type string `json:"type,omitempty"` // Task type, one of 'map' or 'reduce' (optional) + Assets []string `json:"assets,omitempty"` // An array of objects to be placed in the compute zones (optional) + Exec string `json:"exec"` // The actual shell statement to execute + Init string `json:"init"` // Shell statement to execute in each compute zone before any tasks are executed + Count int `json:"count,omitempty"` // If type is 'reduce', an optional number of reducers for this phase (default is 1) + Memory int `json:"memory,omitempty"` // Amount of DRAM to give to your compute zone (in Mb, optional) + Disk int `json:"disk,omitempty"` // Amount of disk space to give to your compute zone (in Gb, optional) +} + +// JobError represents an error occurred during a job execution +type JobError struct { + Id string // Job Id + Phase string // Phase number of the failure + What string // A human readable summary of what failed + Code string // Error code + Message string // Human readable error message + Stderr string // A key that saved the stderr for the given command (optional) + Key string // The input key being processed when the task failed (optional) +} + +// Creates a job with the given options. +// See API docs: http://apidocs.joyent.com/manta/api.html#CreateJob +func (c *Client) CreateJob(opts CreateJobOpts) (string, error) { + var resp string + var respHeader http.Header + req := request{ + method: client.POST, + url: apiJobs, + reqValue: opts, + respHeader: &respHeader, + resp: &resp, + expectedStatus: http.StatusCreated, + } + respData, err := c.sendRequest(req) + if err != nil { + return "", errors.Newf(err, "failed to create job with name: %s", opts.Name) + } + return respData.RespHeaders.Get("Location"), nil +} + +// Submits inputs to an already created job. +// See API docs: http://apidocs.joyent.com/manta/api.html#AddJobInputs +func (c *Client) AddJobInputs(jobId string, jobInputs io.Reader) error { + inputData, errI := ioutil.ReadAll(jobInputs) + if errI != nil { + return errors.Newf(errI, "failed to read inputs for job %s", jobId) + } + requestHeaders := make(http.Header) + requestHeaders.Set("Accept", "*/*") + requestHeaders.Set("Content-Type", "text/plain") + req := request{ + method: client.POST, + url: makeURL(apiJobs, jobId, apiJobsLive, apiJobsIn), + reqValue: string(inputData), + reqHeader: requestHeaders, + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to add inputs to job %s", jobId) + } + return nil +} + +// This closes input for a job, and finalize the job. +// See API docs: http://apidocs.joyent.com/manta/api.html#EndJobInput +func (c *Client) EndJobInputs(jobId string) error { + req := request{ + method: client.POST, + url: makeURL(apiJobs, jobId, apiJobsLive, apiJobsIn, apiJobsEnd), + expectedStatus: http.StatusAccepted, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to end inputs for job %s", jobId) + } + return nil +} + +// This cancels a job from doing any further work. +// Cancellation is asynchronous and "best effort"; there is no guarantee the job will actually stop +// See API docs: http://apidocs.joyent.com/manta/api.html#CancelJob +func (c *Client) CancelJob(jobId string) error { + req := request{ + method: client.POST, + url: makeURL(apiJobs, jobId, apiJobsLive, apiJobsCancel), + expectedStatus: http.StatusAccepted, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to cancel job %s", jobId) + } + return nil +} + +// Returns the list of jobs. +// Note you can filter the set of jobs down to only live jobs by setting the liveOnly flag. +// See API docs: http://apidocs.joyent.com/manta/api.html#ListJobs +func (c *Client) ListJobs(liveOnly bool) ([]Entry, error) { + var resp []Entry + var url string + if liveOnly { + url = fmt.Sprintf("%s?state=running", apiJobs) + } else { + url = apiJobs + } + req := request{ + method: client.GET, + url: url, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to list jobs") + } + return resp, nil +} + +// Gets the high-level job container object for a given job. +// See API docs: http://apidocs.joyent.com/manta/api.html#GetJob +func (c *Client) GetJob(jobId string) (Job, error) { + var resp Job + req := request{ + method: client.GET, + url: makeURL(apiJobs, jobId, apiJobsLive, apiJobsStatus), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return Job{}, errors.Newf(err, "failed to get job with id: %s", jobId) + } + return resp, nil +} + +// Returns the current "live" set of outputs from a given job. +// See API docs: http://apidocs.joyent.com/manta/api.html#GetJobOutput +func (c *Client) GetJobOutput(jobId string) (string, error) { + var resp string + req := request{ + method: client.GET, + url: makeURL(apiJobs, jobId, apiJobsLive, apiJobsOut), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return "", errors.Newf(err, "failed to get output for job with id: %s", jobId) + } + return resp, nil +} + +// Returns the submitted input objects for a given job, available while the job is running. +// See API docs: http://apidocs.joyent.com/manta/api.html#GetJobInput +func (c *Client) GetJobInput(jobId string) (string, error) { + var resp string + req := request{ + method: client.GET, + url: makeURL(apiJobs, jobId, apiJobsLive, apiJobsIn), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return "", errors.Newf(err, "failed to get input for job with id: %s", jobId) + } + return resp, nil +} + +// Returns the current "live" set of failures from a given job. +// See API docs: http://apidocs.joyent.com/manta/api.html#GetJobFailures +func (c *Client) GetJobFailures(jobId string) (interface{}, error) { + var resp interface{} + req := request{ + method: client.GET, + url: makeURL(apiJobs, jobId, apiJobsLive, apiJobsFail), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get failures for job with id: %s", jobId) + } + return resp, nil +} + +// Returns the current "live" set of errors from a given job. +// See API docs: http://apidocs.joyent.com/manta/api.html#GetJobErrors +func (c *Client) GetJobErrors(jobId string) ([]JobError, error) { + var resp []JobError + req := request{ + method: client.GET, + url: makeURL(apiJobs, jobId, apiJobsLive, apiJobsErr), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get errors for job with id: %s", jobId) + } + return resp, nil +} + +// Returns a signed URL to retrieve the object at path. +func (c *Client) SignURL(path string, expires time.Time) (string, error) { + return c.client.SignURL(path, expires) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/manta/manta_test.go juju-core-1.18.0/src/github.com/joyent/gomanta/manta/manta_test.go --- juju-core-1.17.7/src/github.com/joyent/gomanta/manta/manta_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/manta/manta_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,32 @@ +// +// gomanta - Go library to interact with Joyent Manta +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package manta_test + +import ( + "flag" + "github.com/joyent/gocommon/jpc" + gc "launchpad.net/gocheck" + "testing" +) + +var live = flag.Bool("live", false, "Include live Manta tests") +var keyName = flag.String("key.name", "", "Specify the full path to the private key, defaults to ~/.ssh/id_rsa") + +func Test(t *testing.T) { + if *live { + creds, err := jpc.CompleteCredentialsFromEnv(*keyName) + if err != nil { + t.Fatalf("Error setting up test suite: %s", err.Error()) + } + registerMantaTests(creds) + } + registerLocalTests(*keyName) + gc.TestingT(t) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gomanta/README.md juju-core-1.18.0/src/github.com/joyent/gomanta/README.md --- juju-core-1.17.7/src/github.com/joyent/gomanta/README.md 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gomanta/README.md 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,46 @@ +gomanta +======= + +The gomanta package enables Go programs to interact with the Joyent Manta service. + +## Installation + +Use `go-get` to install gomanta +``` +go get github.com/joyent/gomanta +``` + +## Packages + +The gomanta package is structured as follow: + + - gomanta/localservices. This package provides local services to be used for testing. + - gomanta/manta. This package interacts with the Manta API (http://apidocs.joyent.com/manta/). + + +## Documentation + +Documentation can be found on godoc. + +- [http://godoc.org/github.com/joyent/gomanta](http://godoc.org/github.com/joyent/gomanta) +- [http://godoc.org/github.com/joyent/gomanta/localservices] (http://godoc.org/github.com/joyent/gomanta/localservices) +- [http://godoc.org/github.com/joyent/gomanta/manta](http://godoc.org/github.com/joyent/gomanta/manta) + +## Testing + +Make sure you have the dependencies + +``` +go get "launchpad.net/gocheck" +``` + +To Run all tests +``` +go test ./... +``` + +## License +Licensed under [LGPL v3](LICENSE). + +Copyright (c) 2013 Joyent Inc. +Written by Daniele Stroppa \ No newline at end of file diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/cloudapi/cloudapi.go juju-core-1.18.0/src/github.com/joyent/gosdc/cloudapi/cloudapi.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/cloudapi/cloudapi.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/cloudapi/cloudapi.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,1366 @@ +/* +The gosdc/cloudapi package interacts with the Cloud API (http://apidocs.joyent.com/cloudapi/). + +Licensed under LGPL v3. + +Copyright (c) 2013 Joyent Inc. +Written by Daniele Stroppa + +*/ +package cloudapi + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" + "path" + + "github.com/joyent/gocommon/client" + "github.com/joyent/gocommon/errors" + jh "github.com/joyent/gocommon/http" + "github.com/juju/loggo" +) + +const ( + // The default version of the Cloud API to use + DefaultAPIVersion = "~7.0" + + // CloudAPI URL parts + apiKeys = "keys" + apiPackages = "packages" + apiImages = "images" + apiDatacenters = "datacenters" + apiMachines = "machines" + apiMetadata = "metadata" + apiSnapshots = "snapshots" + apiTags = "tags" + apiAnalytics = "analytics" + apiInstrumentations = "instrumentations" + apiInstrumentationsValue = "value" + apiInstrumentationsRaw = "raw" + apiInstrumentationsHeatmap = "heatmap" + apiInstrumentationsImage = "image" + apiInstrumentationsDetails = "details" + apiUsage = "usage" + apiAudit = "audit" + apiFirewallRules = "fwrules" + apiFirewallRulesEnable = "enable" + apiFirewallRulesDisable = "disable" + apiNetworks = "networks" + + // CloudAPI actions + actionExport = "export" + actionStop = "stop" + actionStart = "start" + actionReboot = "reboot" + actionResize = "resize" + actionRename = "rename" + actionEnableFw = "enable_firewall" + actionDisableFw = "disable_firewall" +) + +// Logger for this package +var Logger = loggo.GetLogger("gosdc.cloudapi") + +// Client provides a means to access the Joyent CloudAPI +type Client struct { + client client.Client +} + +// New creates a new Client. +func New(client client.Client) *Client { + return &Client{client} +} + +// Filter represents a filter that can be applied to an API request. +type Filter struct { + v url.Values +} + +// NewFilter creates a new Filter. +func NewFilter() *Filter { + return &Filter{make(url.Values)} +} + +// Set a value for the specified filter. +func (f *Filter) Set(filter, value string) { + f.v.Set(filter, value) +} + +// Add a value for the specified filter. +func (f *Filter) Add(filter, value string) { + f.v.Add(filter, value) +} + +// request represents an API request +type request struct { + method string + url string + filter *Filter + reqValue interface{} + reqHeader http.Header + resp interface{} + respHeader *http.Header + expectedStatus int +} + +// Helper method to send an API request +func (c *Client) sendRequest(req request) (*jh.ResponseData, error) { + request := jh.RequestData{ + ReqValue: req.reqValue, + ReqHeaders: req.reqHeader, + } + if req.filter != nil { + request.Params = &req.filter.v + } + if req.expectedStatus == 0 { + req.expectedStatus = http.StatusOK + } + respData := jh.ResponseData{ + RespValue: req.resp, + RespHeaders: req.respHeader, + ExpectedStatus: []int{req.expectedStatus}, + } + err := c.client.SendRequest(req.method, req.url, "", &request, &respData) + return &respData, err +} + +// Helper method to create the API URL +func makeURL(parts ...string) string { + return path.Join(parts...) +} + +// Key represent a public key +type Key struct { + Name string // Name for the key + Fingerprint string // Key Fingerprint + Key string // OpenSSH formatted public key +} + +/*func (k Key) Equals(other Key) bool { + if k.Name == other.Name && k.Fingerprint == other.Fingerprint && k.Key == other.Key { + return true + } + return false +}*/ + +// CreateKeyOpts represent the option that can be specified +// when creating a new key. +type CreateKeyOpts struct { + Name string `json:"name"` // Name for the key, optional + Key string `json:"key"` // OpenSSH formatted public key +} + +// Returns a list of public keys registered with a specific account. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListKeys +func (c *Client) ListKeys() ([]Key, error) { + var resp []Key + req := request{ + method: client.GET, + url: apiKeys, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of keys") + } + return resp, nil +} + +// Returns the key identified by keyName. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetKey +func (c *Client) GetKey(keyName string) (*Key, error) { + var resp Key + req := request{ + method: client.GET, + url: makeURL(apiKeys, keyName), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get key with name: %s", keyName) + } + return &resp, nil +} + +// Creates a new key with the specified options. +// See API docs: http://apidocs.joyent.com/cloudapi/#CreateKey +func (c *Client) CreateKey(opts CreateKeyOpts) (*Key, error) { + var resp Key + req := request{ + method: client.POST, + url: apiKeys, + reqValue: opts, + resp: &resp, + expectedStatus: http.StatusCreated, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to create key with name: %s", opts.Name) + } + return &resp, nil +} + +// Deletes the key identified by keyName. +// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteKey +func (c *Client) DeleteKey(keyName string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiKeys, keyName), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete key with name: %s", keyName) + } + return nil +} + +// A Package represent a named collections of resources that are used to describe the ‘sizes’ +// of either a smart machine or a virtual machine. +type Package struct { + Name string // Name for the package + Memory int // Memory available (in Mb) + Disk int // Disk space available (in Gb) + Swap int // Swap memory available (in Mb) + VCPUs int // Number of VCPUs for the package + Default bool // Indicates whether this is the default package in the datacenter + Id string // Unique identifier for the package + Version string // Version for the package + Group string // Group this package belongs to + Description string // Human friendly description for the package +} + +// Provides a list of packages available in the datacenter. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListPackages +func (c *Client) ListPackages(filter *Filter) ([]Package, error) { + var resp []Package + req := request{ + method: client.GET, + url: apiPackages, + filter: filter, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of packages") + } + return resp, nil +} + +// Returns the package specified by packageName. NOTE: packageName can specify +// either the package name or package Id. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetPackage +func (c *Client) GetPackage(packageName string) (*Package, error) { + var resp Package + req := request{ + method: client.GET, + url: makeURL(apiPackages, packageName), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get package with name: %s", packageName) + } + return &resp, nil +} + +// Image represent the software packages that will be available on newly provisioned machines +type Image struct { + Id string // Unique identifier for the image + Name string // Image friendly name + OS string // Underlying operating system + Version string // Image version + Type string // Image type, one of 'smartmachine' or 'virtualmachine' + Description string // Image description + Requirements map[string]interface{} // Minimum requirements for provisioning a machine with this image, e.g. 'password' indicates that a password must be provided + Homepage string // URL for a web page including detailed information for this image (new in API version 7.0) + PublishedAt string `json:"published_at"` // Time this image has been made publicly available (new in API version 7.0) + Public string // Indicates if the image is publicly available (new in API version 7.1) + State string // Current image state. One of 'active', 'unactivated', 'disabled', 'creating', 'failed' (new in API version 7.1) + Tags map[string]string // A map of key/value pairs that allows clients to categorize images by any given criteria (new in API version 7.1) + EULA string // URL of the End User License Agreement (EULA) for the image (new in API version 7.1) + ACL []string // An array of account UUIDs given access to a private image. The field is only relevant to private images (new in API version 7.1) + Owner string // The UUID of the user owning the image +} + +// ExportImageOpts represent the option that can be specified +// when exporting an image. +type ExportImageOpts struct { + MantaPath string `json:"manta_path"` // The Manta path prefix to use when exporting the image +} + +// MantaLocation represent the properties that allow a user +// to retrieve the image file and manifest from Manta +type MantaLocation struct { + MantaURL string `json:"manta_url"` // Manta datacenter URL + ImagePath string `json:"image_path"` // Path to the image + ManifestPath string `json:"manifest_path"` // Path to the image manifest +} + +// CreateImageFromMachineOpts represent the option that can be specified +// when creating a new image from an existing machine. +type CreateImageFromMachineOpts struct { + Machine string `json:"machine"` // The machine UUID from which the image is to be created + Name string `json:"name"` // Image name + Version string `json:"version"` // Image version + Description string `json:"description"` // Image description + Homepage string `json:"homepage"` // URL for a web page including detailed information for this image + EULA string `json:"eula"` // URL of the End User License Agreement (EULA) for the image + ACL []string `json:"acl"` // An array of account UUIDs given access to a private image. The field is only relevant to private images + Tags map[string]string `json:"tags"` // A map of key/value pairs that allows clients to categorize images by any given criteria +} + +// Provides a list of images available in the datacenter. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListImages +func (c *Client) ListImages(filter *Filter) ([]Image, error) { + var resp []Image + req := request{ + method: client.GET, + url: apiImages, + filter: filter, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of images") + } + return resp, nil +} + +// Returns the image specified by imageId. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetImage +func (c *Client) GetImage(imageId string) (*Image, error) { + var resp Image + req := request{ + method: client.GET, + url: makeURL(apiImages, imageId), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get image with id: %s", imageId) + } + return &resp, nil +} + +// (Beta) Delete the image specified by imageId. Must be image owner to do so. +// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteImage +func (c *Client) DeleteImage(imageId string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiImages, imageId), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete image with id: %s", imageId) + } + return nil +} + +// (Beta) Exports an image to the specified Manta path. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListImages +func (c *Client) ExportImage(imageId string, opts ExportImageOpts) (*MantaLocation, error) { + var resp MantaLocation + req := request{ + method: client.POST, + url: fmt.Sprintf("%s/%s?action=%s", apiImages, imageId, actionExport), + reqValue: opts, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to export image %s to %s", imageId, opts.MantaPath) + } + return &resp, nil +} + +// (Beta) Create a new custom image from a machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListImages +func (c *Client) CreateImageFromMachine(opts CreateImageFromMachineOpts) (*Image, error) { + var resp Image + req := request{ + method: client.POST, + url: apiImages, + reqValue: opts, + resp: &resp, + expectedStatus: http.StatusCreated, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to create image from machine %s", opts.Machine) + } + return &resp, nil +} + +// Provides a list of all datacenters this cloud is aware of. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListDatacenters +func (c *Client) ListDatacenters() (map[string]interface{}, error) { + var resp map[string]interface{} + req := request{ + method: client.GET, + url: apiDatacenters, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of datcenters") + } + return resp, nil +} + +// Gets an individual datacenter by name. Returns an HTTP redirect to your client, +// the datacenter URL is in the Location header. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetDatacenter +func (c *Client) GetDatacenter(datacenterName string) (string, error) { + var respHeader http.Header + req := request{ + method: client.GET, + url: makeURL(apiDatacenters, datacenterName), + respHeader: &respHeader, + expectedStatus: http.StatusFound, + } + respData, err := c.sendRequest(req) + if err != nil { + return "", errors.Newf(err, "failed to get datacenter with name: %s", datacenterName) + } + return respData.RespHeaders.Get("Location"), nil +} + +// Machine represent a provisioned virtual machines +type Machine struct { + Id string // Unique identifier for the image + Name string // Machine friendly name + Type string // Machine type, one of 'smartmachine' or 'virtualmachine' + State string // Current state of the machine + Dataset string // The dataset URN the machine was provisioned with. For new images/datasets this value will be the dataset id, i.e, same value than the image attribute + Memory int // The amount of memory the machine has (in Mb) + Disk int // The amount of disk the machine has (in Gb) + IPs []string // The IP addresses the machine has + Metadata map[string]string // Map of the machine metadata, e.g. authorized-keys + Tags map[string]string // Map of the machine tags + Created string // When the machine was created + Updated string // When the machine was updated + Package string // The name of the package used to create the machine + Image string // The image id the machine was provisioned with + PrimaryIP string // The primary (public) IP address for the machine + Networks []string // The network IDs for the machine +} + +// Helper method to compare two machines. Ignores state and timestamps. +func (m Machine) Equals(other Machine) bool { + if m.Id == other.Id && m.Name == other.Name && m.Type == other.Type && m.Dataset == other.Dataset && + m.Memory == other.Memory && m.Disk == other.Disk && m.Package == other.Package && m.Image == other.Image && + m.compareIPs(other) && m.compareMetadata(other) { + return true + } + return false +} + +// Helper method to compare two machines IPs +func (m Machine) compareIPs(other Machine) bool { + if len(m.IPs) != len(other.IPs) { + return false + } + for i, v := range m.IPs { + if v != other.IPs[i] { + return false + } + } + return true +} + +// Helper method to compare two machines metadata +func (m Machine) compareMetadata(other Machine) bool { + if len(m.Metadata) != len(other.Metadata) { + return false + } + for k, v := range m.Metadata { + if v != other.Metadata[k] { + return false + } + } + return true +} + +// CreateMachineOpts represent the option that can be specified +// when creating a new machine. +type CreateMachineOpts struct { + Name string `json:"name"` // Machine friendly name, default is a randomly generated name + Package string `json:"package"` // Name of the package to use on provisioning + Image string `json:"image"` // The image UUID + Networks []string `json:"networks"` // Desired networks IDs + Metadata map[string]string `json:"-"` // An arbitrary set of metadata key/value pairs can be set at provision time + Tags map[string]string `json:"-"` // An arbitrary set of tags can be set at provision time + FirewallEnabled bool `json:"firewall_enabled"` // Completely enable or disable firewall for this machine (new in API version 7.0) +} + +// Snapshot represent a point in time state of a machine. +type Snapshot struct { + Name string // Snapshot name + State string // Snapshot state +} + +// SnapshotOpts represent the option that can be specified +// when creating a new machine snapshot. +type SnapshotOpts struct { + Name string `json:"name"` // Snapshot name +} + +// AuditAction represents an action/event accomplished by a machine. +type AuditAction struct { + Action string // Action name + Parameters map[string]interface{} // Original set of parameters sent when the action was requested + Time string // When the action finished + Success string // Either 'yes' or 'no', depending on the action successfulness + Caller Caller // Account requesting the action +} + +// Caller represents an account requesting an action. +type Caller struct { + Type string // Authentication type for the action request. One of 'basic', 'operator', 'signature' or 'token' + User string // When the authentication type is 'basic', this member will be present and include user login + IP string // The IP addresses this from which the action was requested. Not present if type is 'operator' + KeyId string // When authentication type is either 'signature' or 'token', SSH key identifier +} + +// appendJSON marshals the given attribute value and appends it as an encoded value to the given json data. +// The newly encode (attr, value) is inserted just before the closing "}" in the json data. +func appendJSON(data []byte, attr string, value interface{}) ([]byte, error) { + newData, err := json.Marshal(&value) + if err != nil { + return nil, err + } + strData := string(data) + result := fmt.Sprintf(`%s, "%s":%s}`, strData[:len(strData)-1], attr, string(newData)) + return []byte(result), nil +} + +type jsonOpts CreateMachineOpts + +func (opts CreateMachineOpts) MarshalJSON() ([]byte, error) { + var jo jsonOpts = jsonOpts(opts) + data, err := json.Marshal(&jo) + if err != nil { + return nil, err + } + for k, v := range opts.Tags { + data, err = appendJSON(data, k, v) + if err != nil { + return nil, err + } + } + for k, v := range opts.Metadata { + data, err = appendJSON(data, k, v) + if err != nil { + return nil, err + } + } + return data, nil +} + +// Lists all machines on record for an account. +// You can paginate this API by passing in offset, and limit +// See API docs: http://apidocs.joyent.com/cloudapi/#ListMachines +func (c *Client) ListMachines(filter *Filter) ([]Machine, error) { + var resp []Machine + req := request{ + method: client.GET, + url: apiMachines, + filter: filter, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of machines") + } + return resp, nil +} + +// Returns the number of machines on record for an account. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListMachines +func (c *Client) CountMachines() (int, error) { + var resp int + req := request{ + method: client.HEAD, + url: apiMachines, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return -1, errors.Newf(err, "failed to get count of machines") + } + return resp, nil +} + +// Returns the machine specified by machineId. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetMachine +func (c *Client) GetMachine(machineId string) (*Machine, error) { + var resp Machine + req := request{ + method: client.GET, + url: makeURL(apiMachines, machineId), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get machine with id: %s", machineId) + } + return &resp, nil +} + +// Creates a new machine with the options specified. +// See API docs: http://apidocs.joyent.com/cloudapi/#CreateMachine +func (c *Client) CreateMachine(opts CreateMachineOpts) (*Machine, error) { + var resp Machine + req := request{ + method: client.POST, + url: apiMachines, + reqValue: opts, + resp: &resp, + expectedStatus: http.StatusCreated, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to create machine with name: %s", opts.Name) + } + return &resp, nil +} + +// Stops a running machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#StopMachine +func (c *Client) StopMachine(machineId string) error { + req := request{ + method: client.POST, + url: fmt.Sprintf("%s/%s?action=%s", apiMachines, machineId, actionStop), + expectedStatus: http.StatusAccepted, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to stop machine with id: %s", machineId) + } + return nil +} + +// Starts a stopped machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#StartMachine +func (c *Client) StartMachine(machineId string) error { + req := request{ + method: client.POST, + url: fmt.Sprintf("%s/%s?action=%s", apiMachines, machineId, actionStart), + expectedStatus: http.StatusAccepted, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to start machine with id: %s", machineId) + } + return nil +} + +// Reboots (stop followed by a start) a machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#RebootMachine +func (c *Client) RebootMachine(machineId string) error { + req := request{ + method: client.POST, + url: fmt.Sprintf("%s/%s?action=%s", apiMachines, machineId, actionReboot), + expectedStatus: http.StatusAccepted, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to reboot machine with id: %s", machineId) + } + return nil +} + +// Allows you to resize a SmartMachine. Virtual machines can also be resized, +// but only resizing virtual machines to a higher capacity package is supported. +// See API docs: http://apidocs.joyent.com/cloudapi/#ResizeMachine +func (c *Client) ResizeMachine(machineId, packageName string) error { + req := request{ + method: client.POST, + url: fmt.Sprintf("%s/%s?action=%s&package=%s", apiMachines, machineId, actionResize, packageName), + expectedStatus: http.StatusAccepted, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to resize machine with id: %s", machineId) + } + return nil +} + +// Renames an existing machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#RenameMachine +func (c *Client) RenameMachine(machineId, machineName string) error { + req := request{ + method: client.POST, + url: fmt.Sprintf("%s/%s?action=%s&name=%s", apiMachines, machineId, actionRename, machineName), + expectedStatus: http.StatusAccepted, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to rename machine with id: %s", machineId) + } + return nil +} + +// List all the firewall rules for the specified machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListMachineFirewallRules +func (c *Client) ListMachineFirewallRules(machineId string) ([]FirewallRule, error) { + var resp []FirewallRule + req := request{ + method: client.GET, + url: makeURL(apiMachines, machineId, apiFirewallRules), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of firewall rules for machine with id %s", machineId) + } + return resp, nil +} + +// Enable the firewall for the specified machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#EnableMachineFirewall +func (c *Client) EnableFirewallMachine(machineId string) error { + req := request{ + method: client.POST, + url: fmt.Sprintf("%s/%s?action=%s", apiMachines, machineId, actionEnableFw), + expectedStatus: http.StatusAccepted, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to enable firewall on machine with id: %s", machineId) + } + return nil +} + +// Disable the firewall for the specified machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#DisableMachineFirewall +func (c *Client) DisableFirewallMachine(machineId string) error { + req := request{ + method: client.POST, + url: fmt.Sprintf("%s/%s?action=%s", apiMachines, machineId, actionDisableFw), + expectedStatus: http.StatusAccepted, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to disable firewall on machine with id: %s", machineId) + } + return nil +} + +// Creates a new snapshot for the machine with the options specified. +// See API docs: http://apidocs.joyent.com/cloudapi/#CreateMachineSnapshot +func (c *Client) CreateMachineSnapshot(machineId string, opts SnapshotOpts) (*Snapshot, error) { + var resp Snapshot + req := request{ + method: client.POST, + url: makeURL(apiMachines, machineId, apiSnapshots), + reqValue: opts, + resp: &resp, + expectedStatus: http.StatusCreated, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to create snapshot %s from machine with id %s", opts.Name, machineId) + } + return &resp, nil +} + +// Start the machine from the specified snapshot. Machine must be in 'stopped' state. +// See API docs: http://apidocs.joyent.com/cloudapi/#StartMachineFromSnapshot +func (c *Client) StartMachineFromSnapshot(machineId, snapshotName string) error { + req := request{ + method: client.POST, + url: makeURL(apiMachines, machineId, apiSnapshots, snapshotName), + expectedStatus: http.StatusAccepted, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to start machine with id %s from snapshot %s", machineId, snapshotName) + } + return nil +} + +// List all snapshots for the specified machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListMachineSnapshots +func (c *Client) ListMachineSnapshots(machineId string) ([]Snapshot, error) { + var resp []Snapshot + req := request{ + method: client.GET, + url: makeURL(apiMachines, machineId, apiSnapshots), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of snapshots for machine with id %s", machineId) + } + return resp, nil +} + +// Returns the state of the specified snapshot. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetMachineSnapshot +func (c *Client) GetMachineSnapshot(machineId, snapshotName string) (*Snapshot, error) { + var resp Snapshot + req := request{ + method: client.GET, + url: makeURL(apiMachines, machineId, apiSnapshots, snapshotName), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get snapshot %s for machine with id %s", snapshotName, machineId) + } + return &resp, nil +} + +// Deletes the specified snapshot. +// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteMachineSnapshot +func (c *Client) DeleteMachineSnapshot(machineId, snapshotName string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiMachines, machineId, apiSnapshots, snapshotName), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete snapshot %s for machine with id %s", snapshotName, machineId) + } + return nil +} + +// Updates the metadata for a given machine. +// Any metadata keys passed in here are created if they do not exist, and overwritten if they do. +// See API docs: http://apidocs.joyent.com/cloudapi/#UpdateMachineMetadata +func (c *Client) UpdateMachineMetadata(machineId string, metadata map[string]string) (map[string]interface{}, error) { + var resp map[string]interface{} + req := request{ + method: client.POST, + url: makeURL(apiMachines, machineId, apiMetadata), + reqValue: metadata, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to update metadata for machine with id %s", machineId) + } + return resp, nil +} + +// Returns the complete set of metadata associated with the specified machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetMachineMetadata +func (c *Client) GetMachineMetadata(machineId string) (map[string]interface{}, error) { + var resp map[string]interface{} + req := request{ + method: client.GET, + url: makeURL(apiMachines, machineId, apiMetadata), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of metadata for machine with id %s", machineId) + } + return resp, nil +} + +// Deletes a single metadata key from the specified machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteMachineMetadata +func (c *Client) DeleteMachineMetadata(machineId, metadataKey string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiMachines, machineId, apiMetadata, metadataKey), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete metadata with key %s for machine with id %s", metadataKey, machineId) + } + return nil +} + +// Deletes all metadata keys from the specified machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteAllMachineMetadata +func (c *Client) DeleteAllMachineMetadata(machineId string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiMachines, machineId, apiMetadata), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete metadata for machine with id %s", machineId) + } + return nil +} + +// Adds additional tags to the specified machine. +// This API lets you append new tags, not overwrite existing tags. +// See API docs: http://apidocs.joyent.com/cloudapi/#AddMachineTags +func (c *Client) AddMachineTags(machineId string, tags map[string]string) (map[string]string, error) { + var resp map[string]string + req := request{ + method: client.POST, + url: makeURL(apiMachines, machineId, apiTags), + reqValue: tags, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to add tags for machine with id %s", machineId) + } + return resp, nil +} + +// Replaces existing tags for the specified machine. +// This API lets you overwrite existing tags, not append to existing tags. +// See API docs: http://apidocs.joyent.com/cloudapi/#ReplaceMachineTags +func (c *Client) ReplaceMachineTags(machineId string, tags map[string]string) (map[string]string, error) { + var resp map[string]string + req := request{ + method: client.PUT, + url: makeURL(apiMachines, machineId, apiTags), + reqValue: tags, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to replace tags for machine with id %s", machineId) + } + return resp, nil +} + +// Returns the complete set of tags associated with the specified machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListMachineTags +func (c *Client) ListMachineTags(machineId string) (map[string]string, error) { + var resp map[string]string + req := request{ + method: client.GET, + url: makeURL(apiMachines, machineId, apiTags), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of tags for machine with id %s", machineId) + } + return resp, nil +} + +// Returns the value for a single tag on the specified machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetMachineTag +func (c *Client) GetMachineTag(machineId, tagKey string) (string, error) { + var resp string + requestHeaders := make(http.Header) + requestHeaders.Set("Accept", "text/plain") + req := request{ + method: client.GET, + url: makeURL(apiMachines, machineId, apiTags, tagKey), + resp: &resp, + reqHeader: requestHeaders, + } + if _, err := c.sendRequest(req); err != nil { + return "", errors.Newf(err, "failed to get tag %s for machine with id %s", tagKey, machineId) + } + return resp, nil +} + +// Deletes a single tag from the specified machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteMachineTag +func (c *Client) DeleteMachineTag(machineId, tagKey string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiMachines, machineId, apiTags, tagKey), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete tag with key %s for machine with id %s", tagKey, machineId) + } + return nil +} + +// Deletes all tags from the specified machine. +// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteMachineTags +func (c *Client) DeleteMachineTags(machineId string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiMachines, machineId, apiTags), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete tags for machine with id %s", machineId) + } + return nil +} + +// Allows you to completely destroy a machine. Machine must be in the 'stopped' state. +// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteMachine +func (c *Client) DeleteMachine(machineId string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiMachines, machineId), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete machine with id %s", machineId) + } + return nil +} + +// Provides a list of machine's accomplished actions, (sorted from latest to older one). +// See API docs: http://apidocs.joyent.com/cloudapi/#MachineAudit +func (c *Client) MachineAudit(machineId string) ([]AuditAction, error) { + var resp []AuditAction + req := request{ + method: client.GET, + url: makeURL(apiMachines, machineId, apiAudit), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get actions for machine with id %s", machineId) + } + return resp, nil +} + +// Analytics represents the available analytics +type Analytics struct { + Modules map[string]interface{} // Namespace to organize metrics + Fields map[string]interface{} // Fields represent metadata by which data points can be filtered or decomposed + Types map[string]interface{} // Types are used with both metrics and fields for two purposes: to hint to clients at how to best label values, and to distinguish between numeric and discrete quantities. + Metrics map[string]interface{} // Metrics describe quantities which can be measured by the system + Transformations map[string]interface{} // Transformations are post-processing functions that can be applied to data when it's retrieved. +} + +// Instrumentation specify which metric to collect, how frequently to aggregate data (e.g., every second, every hour, etc.) +// how much data to keep (e.g., 10 minutes' worth, 6 months' worth, etc.) and other configuration options +type Instrumentation struct { + Module string `json:"module"` + Stat string `json:"stat"` + Predicate string `json:"predicate"` + Decomposition []string `json:"decomposition"` + ValueDimension int `json:"value-dimenstion"` + ValueArity string `json:"value-arity"` + RetentionTime int `json:"retention-time"` + Granularity int `json:"granularitiy"` + IdleMax int `json:"idle-max"` + Transformations []string `json:"transformations"` + PersistData bool `json:"persist-data"` + Crtime int `json:"crtime"` + ValueScope string `json:"value-scope"` + Id string `json:"id"` + Uris []Uri `json:"uris"` +} + +// Uri represents a Universal Resource Identifier +type Uri struct { + Uri string // Resource identifier + Name string // URI name +} + +// InstrumentationValue represents the data associated to an instrumentation for a point in time +type InstrumentationValue struct { + Value interface{} + Transformations map[string]interface{} + StartTime int + Duration int +} + +// HeatmapOpts represent the option that can be specified +// when retrieving an instrumentation.'s heatmap +type HeatmapOpts struct { + Height int `json:"height"` // Height of the image in pixels + Width int `json:"width"` // Width of the image in pixels + Ymin int `json:"ymin"` // Y-Axis value for the bottom of the image (default: 0) + Ymax int `json:"ymax"` // Y-Axis value for the top of the image (default: auto) + Nbuckets int `json:"nbuckets"` // Number of buckets in the vertical dimension + Selected []string `json:"selected"` // Array of field values to highlight, isolate or exclude + Isolate bool `json:"isolate"` // If true, only draw selected values + Exclude bool `json:"exclude"` // If true, don't draw selected values at all + Hues []string `json:"hues"` // Array of colors for highlighting selected field values + DecomposeAll bool `json:"decompose_all"` // Highlight all field values + X int `json:"x"` + Y int `json:"y"` +} + +// Heatmap represents an instrumentation's heatmap +type Heatmap struct { + BucketTime int `json:"bucket_time"` // Time corresponding to the bucket (Unix seconds) + BucketYmin int `json:"bucket_ymin"` // Minimum y-axis value for the bucket + BucketYmax int `json:"bucket_ymax"` // Maximum y-axis value for the bucket + Present map[string]interface{} `json:"present"` // If the instrumentation defines a discrete decomposition, this property's value is an object whose keys are values of that field and whose values are the number of data points in that bucket for that key + Total int `json:"total"` // The total number of data points in the bucket +} + +// CreateInstrumentationOpts represent the option that can be specified +// when creating a new instrumentation. +type CreateInstrumentationOpts struct { + Clone int `json:"clone"` // An existing instrumentation ID to be cloned + Module string `json:"module"` // Analytics module + Stat string `json:"stat"` // Analytics stat + Predicate string `json:"predicate"` // Instrumentation predicate, must be JSON string + Decomposition string `json:"decomposition"` + Granularity int `json:"granularity"` // Number of seconds between data points (default is 1) + RetentionTime int `json:"retention-time"` // How long to keep this instrumentation data for + PersistData bool `json:"persist-data"` // Whether or not to store this for historical analysis + IdleMax int `json:"idle-max"` // Number of seconds after which if the instrumentation or its data has not been accessed via the API the service may delete the instrumentation and its data +} + +// Retrieves the "schema" for instrumentations that can be created. +// See API docs: http://apidocs.joyent.com/cloudapi/#DescribeAnalytics +func (c *Client) DescribeAnalytics() (*Analytics, error) { + var resp Analytics + req := request{ + method: client.GET, + url: apiAnalytics, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get analytics") + } + return &resp, nil +} + +// Retrieves all currently created instrumentations. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListInstrumentations +func (c *Client) ListInstrumentations() ([]Instrumentation, error) { + var resp []Instrumentation + req := request{ + method: client.GET, + url: makeURL(apiAnalytics, apiInstrumentations), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get instrumentations") + } + return resp, nil +} + +// Retrieves the configuration for the specified instrumentation. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetInstrumentation +func (c *Client) GetInstrumentation(instrumentationId string) (*Instrumentation, error) { + var resp Instrumentation + req := request{ + method: client.GET, + url: makeURL(apiAnalytics, apiInstrumentations, instrumentationId), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get instrumentation with id %s", instrumentationId) + } + return &resp, nil +} + +// Retrieves the data associated to an instrumentation for a point in time. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetInstrumentationValue +func (c *Client) GetInstrumentationValue(instrumentationId string) (*InstrumentationValue, error) { + var resp InstrumentationValue + req := request{ + method: client.GET, + url: makeURL(apiAnalytics, apiInstrumentations, instrumentationId, apiInstrumentationsValue, apiInstrumentationsRaw), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get value for instrumentation with id %s", instrumentationId) + } + return &resp, nil +} + +// Retrieves the specified instrumentation's heatmap. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetInstrumentationHeatmap +func (c *Client) GetInstrumentationHeatmap(instrumentationId string) (*Heatmap, error) { + var resp Heatmap + req := request{ + method: client.GET, + url: makeURL(apiAnalytics, apiInstrumentations, instrumentationId, apiInstrumentationsValue, apiInstrumentationsHeatmap, apiInstrumentationsImage), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get heatmap image for instrumentation with id %s", instrumentationId) + } + return &resp, nil +} + +// Allows you to retrieve the bucket details for a heatmap. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetInstrumentationHeatmapDetails +func (c *Client) GetInstrumentationHeatmapDetails(instrumentationId string) (*Heatmap, error) { + var resp Heatmap + req := request{ + method: client.GET, + url: makeURL(apiAnalytics, apiInstrumentations, instrumentationId, apiInstrumentationsValue, apiInstrumentationsHeatmap, apiInstrumentationsDetails), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get heatmap details for instrumentation with id %s", instrumentationId) + } + return &resp, nil +} + +// Creates an instrumentation. +// You can clone an existing instrumentation by passing in the parameter clone, which should be a numeric id of an existing instrumentation. +// See API docs: http://apidocs.joyent.com/cloudapi/#CreateInstrumentation +func (c *Client) CreateInstrumentation(opts CreateInstrumentationOpts) (*Instrumentation, error) { + var resp Instrumentation + req := request{ + method: client.POST, + url: makeURL(apiAnalytics, apiInstrumentations), + reqValue: opts, + resp: &resp, + expectedStatus: http.StatusCreated, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to create instrumentation") + } + return &resp, nil +} + +// Destroys an instrumentation. +// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteInstrumentation +func (c *Client) DeleteInstrumentation(instrumentationId string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiAnalytics, apiInstrumentations, instrumentationId), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete instrumentation with id %s", instrumentationId) + } + return nil +} + +// FirewallRule represent a firewall rule that can be specifed for a machine. +type FirewallRule struct { + Id string // Unique identifier for the rule + Enabled bool // Whether the rule is enabled or not + Rule string // Firewall rule in the form 'FROM TO ' +} + +// CreateFwRuleOpts represent the option that can be specified +// when creating a new firewall rule. +type CreateFwRuleOpts struct { + Enabled bool `json:"enabled"` // Whether to enable the rule or not + Rule string `json:"rule"` // Firewall rule in the form 'FROM TO ' +} + +// Lists all the firewall rules on record for a specified account. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListFirewallRules +func (c *Client) ListFirewallRules() ([]FirewallRule, error) { + var resp []FirewallRule + req := request{ + method: client.GET, + url: apiFirewallRules, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of firewall rules") + } + return resp, nil +} + +// Returns the specified firewall rule. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetFirewallRule +func (c *Client) GetFirewallRule(fwRuleId string) (*FirewallRule, error) { + var resp FirewallRule + req := request{ + method: client.GET, + url: makeURL(apiFirewallRules, fwRuleId), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get firewall rule with id %s", fwRuleId) + } + return &resp, nil +} + +// Creates the firewall rule with the specified options. +// See API docs: http://apidocs.joyent.com/cloudapi/#CreateFirewallRule +func (c *Client) CreateFirewallRule(opts CreateFwRuleOpts) (*FirewallRule, error) { + var resp FirewallRule + req := request{ + method: client.POST, + url: apiFirewallRules, + reqValue: opts, + resp: &resp, + expectedStatus: http.StatusCreated, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to create firewall rule: %s", opts.Rule) + } + return &resp, nil +} + +// Updates the specified firewall rule. +// See API docs: http://apidocs.joyent.com/cloudapi/#UpdateFirewallRule +func (c *Client) UpdateFirewallRule(fwRuleId string, opts CreateFwRuleOpts) (*FirewallRule, error) { + var resp FirewallRule + req := request{ + method: client.POST, + url: makeURL(apiFirewallRules, fwRuleId), + reqValue: opts, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to update firewall rule with id %s to %s", fwRuleId, opts.Rule) + } + return &resp, nil +} + +// Enables the given firewall rule record if it is disabled. +// See API docs: http://apidocs.joyent.com/cloudapi/#EnableFirewallRule +func (c *Client) EnableFirewallRule(fwRuleId string) (*FirewallRule, error) { + var resp FirewallRule + req := request{ + method: client.POST, + url: makeURL(apiFirewallRules, fwRuleId, apiFirewallRulesEnable), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to enable firewall rule with id %s", fwRuleId) + } + return &resp, nil +} + +// Disables the given firewall rule record if it is enabled. +// See API docs: http://apidocs.joyent.com/cloudapi/#DisableFirewallRule +func (c *Client) DisableFirewallRule(fwRuleId string) (*FirewallRule, error) { + var resp FirewallRule + req := request{ + method: client.POST, + url: makeURL(apiFirewallRules, fwRuleId, apiFirewallRulesDisable), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to disable firewall rule with id %s", fwRuleId) + } + return &resp, nil +} + +// Removes the given firewall rule record from all the required account machines. +// See API docs: http://apidocs.joyent.com/cloudapi/#DeleteFirewallRule +func (c *Client) DeleteFirewallRule(fwRuleId string) error { + req := request{ + method: client.DELETE, + url: makeURL(apiFirewallRules, fwRuleId), + expectedStatus: http.StatusNoContent, + } + if _, err := c.sendRequest(req); err != nil { + return errors.Newf(err, "failed to delete firewall rule with id %s", fwRuleId) + } + return nil +} + +// Return the list of machines affected by the given firewall rule. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListFirewallRuleMachines +func (c *Client) ListFirewallRuleMachines(fwRuleId string) ([]Machine, error) { + var resp []Machine + req := request{ + method: client.GET, + url: makeURL(apiFirewallRules, fwRuleId, apiMachines), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of machines affected by firewall rule wit id %s", fwRuleId) + } + return resp, nil +} + +// Network represents a network available to a given account +type Network struct { + Id string // Unique identifier for the network + Name string // Network name + Public bool // Whether this a public or private (rfc1918) network + Description string // Optional description for this network, when name is not enough +} + +// List all the networks which can be used by the given account. +// See API docs: http://apidocs.joyent.com/cloudapi/#ListNetworks +func (c *Client) ListNetworks() ([]Network, error) { + var resp []Network + req := request{ + method: client.GET, + url: apiNetworks, + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get list of networks") + } + return resp, nil +} + +// Retrieves an individual network record. +// See API docs: http://apidocs.joyent.com/cloudapi/#GetNetwork +func (c *Client) GetNetwork(networkId string) (*Network, error) { + var resp Network + req := request{ + method: client.GET, + url: makeURL(apiNetworks, networkId), + resp: &resp, + } + if _, err := c.sendRequest(req); err != nil { + return nil, errors.Newf(err, "failed to get network with id %s", networkId) + } + return &resp, nil +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/cloudapi/cloudapi_test.go juju-core-1.18.0/src/github.com/joyent/gosdc/cloudapi/cloudapi_test.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/cloudapi/cloudapi_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/cloudapi/cloudapi_test.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,48 @@ +// +// gosdc - Go library to interact with the Joyent CloudAPI +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package cloudapi_test + +import ( + "flag" + gc "launchpad.net/gocheck" + "testing" + + "github.com/joyent/gocommon/jpc" +) + +const ( + testKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDdArXEuyqVPwJ7uT/QLFYrGLposHGKRP4U1YPuXFFYQMa2Mq9cke6c6YYoHpNU3mVjatHp+sicfQHcO9nPMaWXoIn53kWdldvo0brsqGXXaHcQCjCaSooJiMgG4jDWUmnfySOQA0sEAXcktqmePpLsDlih05mORiueAR1Mglrc6TiVvjd8ZTPhZejMzETVusMweIilE+K7cNjQVxwHId5WVjTRAqRCvZXAIcP2+fzDXTmuKWhSdln19bKz5AEp1jU/eg4D4PuQvwynb9A8Ra2SJnOZ2+9cfDVhrbpzVMty4qQU6WblJNjpLnLpkm8w0isYk2Vr13a+1/N941gFcZaZ daniele@lightman.local" + testKeyFingerprint = "6b:06:0c:6b:0b:44:67:97:2c:4f:87:28:28:f3:c6:a9" + packageId = "d6ca9994-53e7-4adf-a818-aadd3c90a916" + localPackageId = "11223344-1212-abab-3434-aabbccddeeff" + packageName = "g3-standard-1-smartos" + localPackageName = "Small" + imageId = "f669428c-a939-11e2-a485-b790efc0f0c1" + localImageId = "12345678-a1a1-b2b2-c3c3-098765432100" + testFwRule = "FROM subnet 10.35.76.0/24 TO subnet 10.35.101.0/24 ALLOW tcp (PORT 80 AND PORT 443)" + testUpdatedFwRule = "FROM subnet 10.35.76.0/24 TO subnet 10.35.101.0/24 ALLOW tcp (port 80 AND port 443 AND port 8080)" + networkId = "42325ea0-eb62-44c1-8eb6-0af3e2f83abc" + localNetworkId = "123abc4d-0011-aabb-2233-ccdd4455" +) + +var live = flag.Bool("live", false, "Include live Joyent Cloud tests") +var keyName = flag.String("key.name", "", "Specify the full path to the private key, defaults to ~/.ssh/id_rsa") + +func Test(t *testing.T) { + if *live { + creds, err := jpc.CompleteCredentialsFromEnv(*keyName) + if err != nil { + t.Fatalf("Error setting up test suite: %s", err.Error()) + } + registerJoyentCloudTests(creds) + } + registerLocalTests(*keyName) + gc.TestingT(t) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/cloudapi/live_test.go juju-core-1.18.0/src/github.com/joyent/gosdc/cloudapi/live_test.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/cloudapi/live_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/cloudapi/live_test.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,759 @@ +// +// gosdc - Go library to interact with the Joyent CloudAPI +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package cloudapi_test + +import ( + "strings" + "time" + + gc "launchpad.net/gocheck" + + "github.com/joyent/gocommon/client" + "github.com/joyent/gosdc/cloudapi" + "github.com/joyent/gosign/auth" +) + +func registerJoyentCloudTests(creds *auth.Credentials) { + gc.Suite(&LiveTests{creds: creds}) +} + +type LiveTests struct { + creds *auth.Credentials + testClient *cloudapi.Client +} + +func (s *LiveTests) SetUpTest(c *gc.C) { + client := client.NewClient(s.creds.SdcEndpoint.URL, cloudapi.DefaultAPIVersion, s.creds, &cloudapi.Logger) + c.Assert(client, gc.NotNil) + s.testClient = cloudapi.New(client) + c.Assert(s.testClient, gc.NotNil) +} + +// Helper method to create a test key in the user account +func (s *LiveTests) createKey(c *gc.C) { + key, err := s.testClient.CreateKey(cloudapi.CreateKeyOpts{Name: "fake-key", Key: testKey}) + c.Assert(err, gc.IsNil) + c.Assert(key, gc.NotNil) + c.Assert(key, gc.DeepEquals, &cloudapi.Key{Name: "fake-key", Fingerprint: testKeyFingerprint, Key: testKey}) +} + +// Helper method to create a test virtual machine in the user account +func (s *LiveTests) createMachine(c *gc.C) *cloudapi.Machine { + machine, err := s.testClient.CreateMachine(cloudapi.CreateMachineOpts{Package: packageName, Image: imageId}) + c.Assert(err, gc.IsNil) + c.Assert(machine, gc.NotNil) + + // wait for machine to be provisioned + for !s.pollMachineState(c, machine.Id, "running") { + time.Sleep(1 * time.Second) + } + + return machine +} + +// Helper method to create a test virtual machine in the user account with the specified tags +func (s *LiveTests) createMachineWithTags(c *gc.C, tags map[string]string) *cloudapi.Machine { + machine, err := s.testClient.CreateMachine(cloudapi.CreateMachineOpts{Package: packageName, Image: imageId, Tags: tags}) + c.Assert(err, gc.IsNil) + c.Assert(machine, gc.NotNil) + + // wait for machine to be provisioned + for !s.pollMachineState(c, machine.Id, "running") { + time.Sleep(1 * time.Second) + } + + return machine +} + +// Helper method to test the state of a given VM +func (s *LiveTests) pollMachineState(c *gc.C, machineId, state string) bool { + machineConfig, err := s.testClient.GetMachine(machineId) + c.Assert(err, gc.IsNil) + return strings.EqualFold(machineConfig.State, state) +} + +// Helper method to delete a test virtual machine once the test has executed +func (s *LiveTests) deleteMachine(c *gc.C, machineId string) { + err := s.testClient.StopMachine(machineId) + c.Assert(err, gc.IsNil) + + // wait for machine to be stopped + for !s.pollMachineState(c, machineId, "stopped") { + time.Sleep(1 * time.Second) + } + + err = s.testClient.DeleteMachine(machineId) + c.Assert(err, gc.IsNil) +} + +// Helper method to list virtual machine according to the specified filter +func (s *LiveTests) listMachines(c *gc.C, filter *cloudapi.Filter) { + var contains bool + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + machines, err := s.testClient.ListMachines(filter) + c.Assert(err, gc.IsNil) + c.Assert(machines, gc.NotNil) + for _, m := range machines { + if m.Id == testMachine.Id { + contains = true + break + } + } + + // result + if !contains { + c.Fatalf("Obtained machines [%s] do not contain test machine [%s]", machines, *testMachine) + } +} + +// Helper method to create a snapshot of a test virtual machine +func (s *LiveTests) createMachineSnapshot(c *gc.C, machineId string) string { + // generates a unique snapshot name using the current timestamp + t := time.Now() + snapshotName := "test-machine-snapshot-" + t.Format("20060102_150405") + snapshot, err := s.testClient.CreateMachineSnapshot(machineId, cloudapi.SnapshotOpts{Name: snapshotName}) + c.Assert(err, gc.IsNil) + c.Assert(snapshot, gc.NotNil) + c.Assert(snapshot, gc.DeepEquals, &cloudapi.Snapshot{Name: snapshotName, State: "queued"}) + + return snapshotName +} + +// Helper method to create a test firewall rule +func (s *LiveTests) createFirewallRule(c *gc.C) *cloudapi.FirewallRule { + fwRule, err := s.testClient.CreateFirewallRule(cloudapi.CreateFwRuleOpts{Enabled: false, Rule: testFwRule}) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) + c.Assert(fwRule.Rule, gc.Equals, testFwRule) + c.Assert(fwRule.Enabled, gc.Equals, false) + time.Sleep(10 * time.Second) + + return fwRule +} + +// Helper method to a test firewall rule +func (s *LiveTests) deleteFwRule(c *gc.C, fwRuleId string) { + err := s.testClient.DeleteFirewallRule(fwRuleId) + c.Assert(err, gc.IsNil) +} + +// Keys API +func (s *LiveTests) TestCreateKey(c *gc.C) { + s.createKey(c) +} + +func (s *LiveTests) TestListKeys(c *gc.C) { + s.createKey(c) + + keys, err := s.testClient.ListKeys() + c.Assert(err, gc.IsNil) + c.Assert(keys, gc.NotNil) + fakeKey := cloudapi.Key{Name: "fake-key", Fingerprint: testKeyFingerprint, Key: testKey} + for _, k := range keys { + if c.Check(k, gc.DeepEquals, fakeKey) { + c.SucceedNow() + } + } + c.Fatalf("Obtained keys [%s] do not contain test key [%s]", keys, fakeKey) +} + +func (s *LiveTests) TestGetKeyByName(c *gc.C) { + s.createKey(c) + + key, err := s.testClient.GetKey("fake-key") + c.Assert(err, gc.IsNil) + c.Assert(key, gc.NotNil) + c.Assert(key, gc.DeepEquals, &cloudapi.Key{Name: "fake-key", Fingerprint: testKeyFingerprint, Key: testKey}) +} + +func (s *LiveTests) TestGetKeyByFingerprint(c *gc.C) { + s.createKey(c) + + key, err := s.testClient.GetKey(testKeyFingerprint) + c.Assert(err, gc.IsNil) + c.Assert(key, gc.NotNil) + c.Assert(key, gc.DeepEquals, &cloudapi.Key{Name: "fake-key", Fingerprint: testKeyFingerprint, Key: testKey}) +} + +func (s *LiveTests) TestDeleteKey(c *gc.C) { + s.createKey(c) + + err := s.testClient.DeleteKey("fake-key") + c.Assert(err, gc.IsNil) +} + +// Packages API +func (s *LiveTests) TestListPackages(c *gc.C) { + pkgs, err := s.testClient.ListPackages(nil) + c.Assert(err, gc.IsNil) + c.Assert(pkgs, gc.NotNil) + for _, pkg := range pkgs { + c.Check(pkg.Name, gc.FitsTypeOf, string("")) + c.Check(pkg.Memory, gc.FitsTypeOf, int(0)) + c.Check(pkg.Disk, gc.FitsTypeOf, int(0)) + c.Check(pkg.Swap, gc.FitsTypeOf, int(0)) + c.Check(pkg.VCPUs, gc.FitsTypeOf, int(0)) + c.Check(pkg.Default, gc.FitsTypeOf, bool(false)) + c.Check(pkg.Id, gc.FitsTypeOf, string("")) + c.Check(pkg.Version, gc.FitsTypeOf, string("")) + c.Check(pkg.Description, gc.FitsTypeOf, string("")) + c.Check(pkg.Group, gc.FitsTypeOf, string("")) + } +} + +func (s *LiveTests) TestListPackagesWithFilter(c *gc.C) { + filter := cloudapi.NewFilter() + filter.Set("memory", "1024") + pkgs, err := s.testClient.ListPackages(filter) + c.Assert(err, gc.IsNil) + c.Assert(pkgs, gc.NotNil) + for _, pkg := range pkgs { + c.Check(pkg.Name, gc.FitsTypeOf, string("")) + c.Check(pkg.Memory, gc.Equals, 1024) + c.Check(pkg.Disk, gc.FitsTypeOf, int(0)) + c.Check(pkg.Swap, gc.FitsTypeOf, int(0)) + c.Check(pkg.VCPUs, gc.FitsTypeOf, int(0)) + c.Check(pkg.Default, gc.FitsTypeOf, bool(false)) + c.Check(pkg.Id, gc.FitsTypeOf, string("")) + c.Check(pkg.Version, gc.FitsTypeOf, string("")) + c.Check(pkg.Description, gc.FitsTypeOf, string("")) + c.Check(pkg.Group, gc.FitsTypeOf, string("")) + } +} + +func (s *LiveTests) TestGetPackageFromName(c *gc.C) { + key, err := s.testClient.GetPackage(packageName) + c.Assert(err, gc.IsNil) + c.Assert(key, gc.NotNil) + c.Assert(key, gc.DeepEquals, &cloudapi.Package{ + Name: packageName, + Memory: 1024, + Disk: 33792, + Swap: 2048, + VCPUs: 0, + Default: false, + Id: packageId, + Version: "1.0.0", + Description: "Standard 1 GB RAM 0.25 vCPU and bursting 33 GB Disk", + Group: "Standard", + }) +} + +func (s *LiveTests) TestGetPackageFromId(c *gc.C) { + key, err := s.testClient.GetPackage(packageId) + c.Assert(err, gc.IsNil) + c.Assert(key, gc.NotNil) + c.Assert(key, gc.DeepEquals, &cloudapi.Package{ + Name: packageName, + Memory: 1024, + Disk: 33792, + Swap: 2048, + VCPUs: 0, + Default: false, + Id: packageId, + Version: "1.0.0", + Description: "Standard 1 GB RAM 0.25 vCPU and bursting 33 GB Disk", + Group: "Standard", + }) +} + +// Images API +func (s *LiveTests) TestListImages(c *gc.C) { + imgs, err := s.testClient.ListImages(nil) + c.Assert(err, gc.IsNil) + c.Assert(imgs, gc.NotNil) + for _, img := range imgs { + c.Check(img.Id, gc.FitsTypeOf, string("")) + c.Check(img.Name, gc.FitsTypeOf, string("")) + c.Check(img.OS, gc.FitsTypeOf, string("")) + c.Check(img.Version, gc.FitsTypeOf, string("")) + c.Check(img.Type, gc.FitsTypeOf, string("")) + c.Check(img.Description, gc.FitsTypeOf, string("")) + c.Check(img.Requirements, gc.FitsTypeOf, map[string]interface{}{"key": "value"}) + c.Check(img.Homepage, gc.FitsTypeOf, string("")) + c.Check(img.PublishedAt, gc.FitsTypeOf, string("")) + c.Check(img.Public, gc.FitsTypeOf, string("")) + c.Check(img.State, gc.FitsTypeOf, string("")) + c.Check(img.Tags, gc.FitsTypeOf, map[string]string{"key": "value"}) + c.Check(img.EULA, gc.FitsTypeOf, string("")) + c.Check(img.ACL, gc.FitsTypeOf, []string{"", ""}) + } +} + +func (s *LiveTests) TestListImagesWithFilter(c *gc.C) { + filter := cloudapi.NewFilter() + filter.Set("os", "smartos") + imgs, err := s.testClient.ListImages(filter) + c.Assert(err, gc.IsNil) + c.Assert(imgs, gc.NotNil) + for _, img := range imgs { + c.Check(img.Id, gc.FitsTypeOf, string("")) + c.Check(img.Name, gc.FitsTypeOf, string("")) + c.Check(img.OS, gc.Equals, "smartos") + c.Check(img.Version, gc.FitsTypeOf, string("")) + c.Check(img.Type, gc.FitsTypeOf, string("")) + c.Check(img.Description, gc.FitsTypeOf, string("")) + c.Check(img.Requirements, gc.FitsTypeOf, map[string]interface{}{"key": "value"}) + c.Check(img.Homepage, gc.FitsTypeOf, string("")) + c.Check(img.PublishedAt, gc.FitsTypeOf, string("")) + c.Check(img.Public, gc.FitsTypeOf, string("")) + c.Check(img.State, gc.FitsTypeOf, string("")) + c.Check(img.Tags, gc.FitsTypeOf, map[string]string{"key": "value"}) + c.Check(img.EULA, gc.FitsTypeOf, string("")) + c.Check(img.ACL, gc.FitsTypeOf, []string{"", ""}) + } +} + +// TODO Add test for deleteImage, exportImage and CreateMachineFormIMage + +func (s *LiveTests) TestGetImage(c *gc.C) { + requirements := map[string]interface{}{} + img, err := s.testClient.GetImage(imageId) + c.Assert(err, gc.IsNil) + c.Assert(img, gc.NotNil) + c.Assert(img, gc.DeepEquals, &cloudapi.Image{ + Id: imageId, + Name: "base", + Version: "13.1.0", + OS: "smartos", + Type: "smartmachine", + Description: "A 32-bit SmartOS image with just essential packages installed. Ideal for users who are comfortable with setting up their own environment and tools.", + Requirements: requirements, + PublishedAt: "2013-04-26T15:16:02Z", + }) +} + +// Datacenter API +func (s *LiveTests) TestListDatacenters(c *gc.C) { + dcs, err := s.testClient.ListDatacenters() + c.Assert(err, gc.IsNil) + c.Assert(dcs, gc.HasLen, 4) + c.Assert(dcs["us-west-1"], gc.Equals, "https://us-west-1.api.joyentcloud.com") +} + +func (s *LiveTests) TestGetDatacenter(c *gc.C) { + dc, err := s.testClient.GetDatacenter("us-west-1") + c.Assert(err, gc.IsNil) + c.Assert(dc, gc.Equals, "https://us-west-1.api.joyentcloud.com") +} + +func (s *LiveTests) TestCreateMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + c.Assert(testMachine.Type, gc.Equals, "smartmachine") + c.Assert(testMachine.Dataset, gc.Equals, "sdc:sdc:base:13.1.0") + c.Assert(testMachine.Memory, gc.Equals, 1024) + c.Assert(testMachine.Disk, gc.Equals, 33792) + c.Assert(testMachine.Package, gc.Equals, packageName) + c.Assert(testMachine.Image, gc.Equals, imageId) +} + +func (s *LiveTests) TestCreateMachineWithTags(c *gc.C) { + tags := map[string]string{"tag.tag1": "value1", "tag.tag2": "value2"} + testMachine := s.createMachineWithTags(c, tags) + defer s.deleteMachine(c, testMachine.Id) + + c.Assert(testMachine.Type, gc.Equals, "smartmachine") + c.Assert(testMachine.Dataset, gc.Equals, "sdc:sdc:base:13.1.0") + c.Assert(testMachine.Memory, gc.Equals, 1024) + c.Assert(testMachine.Disk, gc.Equals, 33792) + c.Assert(testMachine.Package, gc.Equals, packageName) + c.Assert(testMachine.Image, gc.Equals, imageId) + c.Assert(testMachine.Tags, gc.DeepEquals, map[string]string{"tag1": "value1", "tag2": "value2"}) +} + +func (s *LiveTests) TestListMachines(c *gc.C) { + s.listMachines(c, nil) +} + +func (s *LiveTests) TestListMachinesWithFilter(c *gc.C) { + filter := cloudapi.NewFilter() + filter.Set("memory", "1024") + + s.listMachines(c, filter) +} + +func (s *LiveTests) TestCountMachines(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + count, err := s.testClient.CountMachines() + c.Assert(err, gc.IsNil) + c.Assert(count >= 1, gc.Equals, true) +} + +func (s *LiveTests) TestGetMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + machine, err := s.testClient.GetMachine(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(machine, gc.NotNil) + c.Assert(machine.Equals(*testMachine), gc.Equals, true) +} + +func (s *LiveTests) TestStopMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.StopMachine(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestStartMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.StopMachine(testMachine.Id) + c.Assert(err, gc.IsNil) + + // wait for machine to be stopped + for !s.pollMachineState(c, testMachine.Id, "stopped") { + time.Sleep(1 * time.Second) + } + + err = s.testClient.StartMachine(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestRebootMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.RebootMachine(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestRenameMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.RenameMachine(testMachine.Id, "test-machine-renamed") + c.Assert(err, gc.IsNil) + + renamed, err := s.testClient.GetMachine(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(renamed.Name, gc.Equals, "test-machine-renamed") +} + +func (s *LiveTests) TestResizeMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.ResizeMachine(testMachine.Id, "g3-standard-1.75-smartos") + c.Assert(err, gc.IsNil) + + resized, err := s.testClient.GetMachine(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(resized.Package, gc.Equals, "g3-standard-1.75-smartos") +} + +func (s *LiveTests) TestListMachinesFirewallRules(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + fwRules, err := s.testClient.ListMachineFirewallRules(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRules, gc.NotNil) +} + +func (s *LiveTests) TestEnableFirewallMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.EnableFirewallMachine(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestDisableFirewallMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.DisableFirewallMachine(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestCreateMachineSnapshot(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + s.createMachineSnapshot(c, testMachine.Id) +} + +func (s *LiveTests) TestStartMachineFromShapshot(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + snapshotName := s.createMachineSnapshot(c, testMachine.Id) + + err := s.testClient.StopMachine(testMachine.Id) + c.Assert(err, gc.IsNil) + + // wait for machine to be stopped + for !s.pollMachineState(c, testMachine.Id, "stopped") { + time.Sleep(1 * time.Second) + } + + err = s.testClient.StartMachineFromSnapshot(testMachine.Id, snapshotName) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestListMachineSnapshots(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + s.createMachineSnapshot(c, testMachine.Id) + + snapshots, err := s.testClient.ListMachineSnapshots(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(snapshots, gc.HasLen, 1) +} + +func (s *LiveTests) TestGetMachineSnapshot(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + snapshotName := s.createMachineSnapshot(c, testMachine.Id) + + snapshot, err := s.testClient.GetMachineSnapshot(testMachine.Id, snapshotName) + c.Assert(err, gc.IsNil) + c.Assert(snapshot, gc.NotNil) + c.Assert(snapshot, gc.DeepEquals, &cloudapi.Snapshot{Name: snapshotName, State: "created"}) +} + +func (s *LiveTests) TestDeleteMachineSnapshot(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + snapshotName := s.createMachineSnapshot(c, testMachine.Id) + + err := s.testClient.DeleteMachineSnapshot(testMachine.Id, snapshotName) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestUpdateMachineMetadata(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + md, err := s.testClient.UpdateMachineMetadata(testMachine.Id, map[string]string{"test-metadata": "md value", "test": "test"}) + metadata := map[string]interface{}{"root_authorized_keys": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyucy41MNcPxEkNUneeRT0j4mXX5zzW4cFYZ0G/Wpqrb/A+JZV6xlwmAwsGPAvebeEp40CSc0gzauR0nsQ+0Hefdp+dHdEEZlZ7WhedknponA8cQURU38cmTnGweaw2B0+vkULo5AUPAjv0Y1nGPZVlKWNeR6NJhq51pEtj4eLYCJ+kylHEIjQbP5Q1LQHxxotoY29N/xMx+ZVYGprHUJ5ihMOC1nrz2kqUjbCvvMLC0yzAI3vfKtL14BQs9Aq9ggl9oZZylmsgy9CnrPa5t98/wqG+snGyrPSL27km0rll1Jz6xcraGkXQP0adFJxw7mFrXItAt6TUyAuLoohhjHd daniele@lightman.local\n", + "origin": "cloudapi", "creator_uuid": "ff0c4a2b-f89a-4f14-81ee-5b31e7c89ece", "test": "test", "test-metadata": "md value", + "context": map[string]interface{}{"caller": map[string]interface{}{"type": "signature", "ip": "127.0.0.1", "keyId": "/dstroppa/keys/12:c3:a7:cb:a2:29:e2:90:88:3f:04:53:3b:4e:75:40"}, + "params": map[string]interface{}{"account": "dstroppa", "machine": testMachine.Id, "test": "test", "test-metadata": "md value"}}} + c.Assert(err, gc.IsNil) + c.Assert(md, gc.NotNil) + c.Assert(md, gc.DeepEquals, metadata) +} + +func (s *LiveTests) TestGetMachineMetadata(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + md, err := s.testClient.GetMachineMetadata(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(md, gc.NotNil) + c.Assert(md, gc.HasLen, 5) +} + +func (s *LiveTests) TestDeleteMachineMetadata(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + _, err := s.testClient.UpdateMachineMetadata(testMachine.Id, map[string]string{"test-metadata": "md value"}) + c.Assert(err, gc.IsNil) + // allow update to propagate + time.Sleep(10 * time.Second) + + err = s.testClient.DeleteMachineMetadata(testMachine.Id, "test-metadata") + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestDeleteAllMachineMetadata(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.DeleteAllMachineMetadata(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestAddMachineTags(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + machineTags := map[string]string{"test-tag": "test-tag-value", "test": "test", "tag1": "tagtag"} + tags, err := s.testClient.AddMachineTags(testMachine.Id, map[string]string{"test-tag": "test-tag-value", "test": "test", "tag1": "tagtag"}) + c.Assert(err, gc.IsNil) + c.Assert(tags, gc.NotNil) + c.Assert(tags, gc.DeepEquals, machineTags) +} + +func (s *LiveTests) TestReplaceMachineTags(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + machineTags := map[string]string{"origin": "cloudapi", "creator_uuid": "ff0c4a2b-f89a-4f14-81ee-5b31e7c89ece", "test-tag": "test-tag-value", "test": "test tag", "tag1": "tag2"} + tags, err := s.testClient.ReplaceMachineTags(testMachine.Id, map[string]string{"test-tag": "test-tag-value", "test": "test tag", "tag1": "tag2"}) + c.Assert(err, gc.IsNil) + c.Assert(tags, gc.NotNil) + c.Assert(tags, gc.DeepEquals, machineTags) +} + +func (s *LiveTests) TestListMachineTags(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + tags, err := s.testClient.ListMachineTags(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(tags, gc.NotNil) + c.Assert(tags, gc.HasLen, 5) +} + +func (s *LiveTests) TestGetMachineTag(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + _, err := s.testClient.AddMachineTags(testMachine.Id, map[string]string{"test-tag": "test-tag-value"}) + c.Assert(err, gc.IsNil) + // allow update to propagate + time.Sleep(15 * time.Second) + + tag, err := s.testClient.GetMachineTag(testMachine.Id, "test-tag") + c.Assert(err, gc.IsNil) + c.Assert(tag, gc.NotNil) + c.Assert(tag, gc.Equals, "test-tag-value") +} + +func (s *LiveTests) TestDeleteMachineTag(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + _, err := s.testClient.AddMachineTags(testMachine.Id, map[string]string{"test-tag": "test-tag-value"}) + c.Assert(err, gc.IsNil) + // allow update to propagate + time.Sleep(15 * time.Second) + + err = s.testClient.DeleteMachineTag(testMachine.Id, "test-tag") + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestDeleteMachineTags(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.DeleteMachineTags(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LiveTests) TestMachineAudit(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + actions, err := s.testClient.MachineAudit(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(actions, gc.NotNil) + c.Assert(len(actions) > 0, gc.Equals, true) +} + +func (s *LiveTests) TestDeleteMachine(c *gc.C) { + testMachine := s.createMachine(c) + + s.deleteMachine(c, testMachine.Id) +} + +// Analytics API + +// FirewallRules API + +func (s *LiveTests) TestCreateFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + // cleanup + s.deleteFwRule(c, testFwRule.Id) +} + +func (s *LiveTests) TestListFirewallRules(c *gc.C) { + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + rules, err := s.testClient.ListFirewallRules() + c.Assert(err, gc.IsNil) + c.Assert(rules, gc.NotNil) +} + +func (s *LiveTests) TestGetFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + fwRule, err := s.testClient.GetFirewallRule(testFwRule.Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) + c.Assert((*fwRule), gc.DeepEquals, (*testFwRule)) +} + +func (s *LiveTests) TestUpdateFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + fwRule, err := s.testClient.UpdateFirewallRule(testFwRule.Id, cloudapi.CreateFwRuleOpts{Rule: testUpdatedFwRule}) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) + c.Assert(fwRule.Rule, gc.Equals, testUpdatedFwRule) +} + +func (s *LiveTests) TestEnableFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + fwRule, err := s.testClient.EnableFirewallRule((*testFwRule).Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) +} + +func (s *LiveTests) TestListFirewallRuleMachines(c *gc.C) { + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + machines, err := s.testClient.ListFirewallRuleMachines((*testFwRule).Id) + c.Assert(err, gc.IsNil) + c.Assert(machines, gc.NotNil) +} + +func (s *LiveTests) TestDisableFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + fwRule, err := s.testClient.DisableFirewallRule((*testFwRule).Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) +} + +func (s *LiveTests) TestDeleteFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + s.deleteFwRule(c, testFwRule.Id) +} + +// Networks API +func (s *LiveTests) TestListNetworks(c *gc.C) { + nets, err := s.testClient.ListNetworks() + c.Assert(err, gc.IsNil) + c.Assert(nets, gc.NotNil) +} + +func (s *LiveTests) TestGetNetwork(c *gc.C) { + net, err := s.testClient.GetNetwork(networkId) + c.Assert(err, gc.IsNil) + c.Assert(net, gc.NotNil) + c.Assert(net, gc.DeepEquals, &cloudapi.Network{ + Id: networkId, + Name: "Joyent-SDC-Public", + Public: true, + Description: "", + }) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/cloudapi/local_test.go juju-core-1.18.0/src/github.com/joyent/gosdc/cloudapi/local_test.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/cloudapi/local_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/cloudapi/local_test.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,564 @@ +// +// gosdc - Go library to interact with the Joyent CloudAPI +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package cloudapi_test + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "os" + "strings" + "time" + + gc "launchpad.net/gocheck" + + "github.com/joyent/gocommon/client" + "github.com/joyent/gosdc/cloudapi" + lc "github.com/joyent/gosdc/localservices/cloudapi" + "github.com/joyent/gosign/auth" +) + +var privateKey []byte + +func registerLocalTests(keyName string) { + var localKeyFile string + if keyName == "" { + localKeyFile = os.Getenv("HOME") + "/.ssh/id_rsa" + } else { + localKeyFile = keyName + } + privateKey, _ = ioutil.ReadFile(localKeyFile) + + gc.Suite(&LocalTests{}) +} + +type LocalTests struct { + //LocalTests + creds *auth.Credentials + testClient *cloudapi.Client + Server *httptest.Server + Mux *http.ServeMux + oldHandler http.Handler + cloudapi *lc.CloudAPI +} + +func (s *LocalTests) SetUpSuite(c *gc.C) { + // Set up the HTTP server. + s.Server = httptest.NewServer(nil) + s.oldHandler = s.Server.Config.Handler + s.Mux = http.NewServeMux() + s.Server.Config.Handler = s.Mux + + // Set up a Joyent CloudAPI service. + authentication := auth.Auth{User: "localtest", PrivateKey: string(privateKey), Algorithm: "rsa-sha256"} + + s.creds = &auth.Credentials{ + UserAuthentication: authentication, + SdcKeyId: "", + SdcEndpoint: auth.Endpoint{URL: s.Server.URL}, + } + s.cloudapi = lc.New(s.creds.SdcEndpoint.URL, s.creds.UserAuthentication.User) + s.cloudapi.SetupHTTP(s.Mux) +} + +func (s *LocalTests) TearDownSuite(c *gc.C) { + s.Mux = nil + s.Server.Config.Handler = s.oldHandler + s.Server.Close() +} + +func (s *LocalTests) SetUpTest(c *gc.C) { + client := client.NewClient(s.creds.SdcEndpoint.URL, cloudapi.DefaultAPIVersion, s.creds, &cloudapi.Logger) + c.Assert(client, gc.NotNil) + s.testClient = cloudapi.New(client) + c.Assert(s.testClient, gc.NotNil) +} + +// Helper method to create a test key in the user account +func (s *LocalTests) createKey(c *gc.C) { + key, err := s.testClient.CreateKey(cloudapi.CreateKeyOpts{Name: "fake-key", Key: testKey}) + c.Assert(err, gc.IsNil) + c.Assert(key, gc.DeepEquals, &cloudapi.Key{Name: "fake-key", Fingerprint: "", Key: testKey}) +} + +func (s *LocalTests) deleteKey(c *gc.C) { + err := s.testClient.DeleteKey("fake-key") + c.Assert(err, gc.IsNil) +} + +// Helper method to create a test virtual machine in the user account +func (s *LocalTests) createMachine(c *gc.C) *cloudapi.Machine { + machine, err := s.testClient.CreateMachine(cloudapi.CreateMachineOpts{Package: localPackageName, Image: localImageId}) + c.Assert(err, gc.IsNil) + c.Assert(machine, gc.NotNil) + + // wait for machine to be provisioned + for !s.pollMachineState(c, machine.Id, "running") { + time.Sleep(1 * time.Second) + } + + return machine +} + +// Helper method to test the state of a given VM +func (s *LocalTests) pollMachineState(c *gc.C, machineId, state string) bool { + machineConfig, err := s.testClient.GetMachine(machineId) + c.Assert(err, gc.IsNil) + return strings.EqualFold(machineConfig.State, state) +} + +// Helper method to delete a test virtual machine once the test has executed +func (s *LocalTests) deleteMachine(c *gc.C, machineId string) { + err := s.testClient.StopMachine(machineId) + c.Assert(err, gc.IsNil) + + // wait for machine to be stopped + for !s.pollMachineState(c, machineId, "stopped") { + time.Sleep(1 * time.Second) + } + + err = s.testClient.DeleteMachine(machineId) + c.Assert(err, gc.IsNil) +} + +// Helper method to list virtual machine according to the specified filter +func (s *LocalTests) listMachines(c *gc.C, filter *cloudapi.Filter) { + var contains bool + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + machines, err := s.testClient.ListMachines(filter) + c.Assert(err, gc.IsNil) + c.Assert(machines, gc.NotNil) + for _, m := range machines { + if m.Id == testMachine.Id { + contains = true + break + } + } + + // result + if !contains { + c.Fatalf("Obtained machines [%s] do not contain test machine [%s]", machines, *testMachine) + } +} + +// Helper method to create a test firewall rule +func (s *LocalTests) createFirewallRule(c *gc.C) *cloudapi.FirewallRule { + fwRule, err := s.testClient.CreateFirewallRule(cloudapi.CreateFwRuleOpts{Enabled: false, Rule: testFwRule}) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) + c.Assert(fwRule.Rule, gc.Equals, testFwRule) + c.Assert(fwRule.Enabled, gc.Equals, false) + time.Sleep(10 * time.Second) + + return fwRule +} + +// Helper method to a test firewall rule +func (s *LocalTests) deleteFwRule(c *gc.C, fwRuleId string) { + err := s.testClient.DeleteFirewallRule(fwRuleId) + c.Assert(err, gc.IsNil) +} + +// Keys API +func (s *LocalTests) TestCreateKey(c *gc.C) { + s.createKey(c) + s.deleteKey(c) +} + +func (s *LocalTests) TestListKeys(c *gc.C) { + s.createKey(c) + defer s.deleteKey(c) + + keys, err := s.testClient.ListKeys() + c.Assert(err, gc.IsNil) + c.Assert(keys, gc.NotNil) + fakeKey := cloudapi.Key{Name: "fake-key", Fingerprint: "", Key: testKey} + for _, k := range keys { + if c.Check(k, gc.DeepEquals, fakeKey) { + c.SucceedNow() + } + } + c.Fatalf("Obtained keys [%s] do not contain test key [%s]", keys, fakeKey) +} + +func (s *LocalTests) TestGetKeyByName(c *gc.C) { + s.createKey(c) + defer s.deleteKey(c) + + key, err := s.testClient.GetKey("fake-key") + c.Assert(err, gc.IsNil) + c.Assert(key, gc.NotNil) + c.Assert(key, gc.DeepEquals, &cloudapi.Key{Name: "fake-key", Fingerprint: "", Key: testKey}) +} + +/*func (s *LocalTests) TestGetKeyByFingerprint(c *gc.C) { + s.createKey(c) + defer s.deleteKey(c) + + key, err := s.testClient.GetKey(testKeyFingerprint) + c.Assert(err, gc.IsNil) + c.Assert(key, gc.NotNil) + c.Assert(key, gc.DeepEquals, &cloudapi.Key{Name: "fake-key", Fingerprint: testKeyFingerprint, Key: testKey}) +} */ + +func (s *LocalTests) TestDeleteKey(c *gc.C) { + s.createKey(c) + + s.deleteKey(c) +} + +// Packages API +func (s *LocalTests) TestListPackages(c *gc.C) { + pkgs, err := s.testClient.ListPackages(nil) + c.Assert(err, gc.IsNil) + c.Assert(pkgs, gc.NotNil) + for _, pkg := range pkgs { + c.Check(pkg.Name, gc.FitsTypeOf, string("")) + c.Check(pkg.Memory, gc.FitsTypeOf, int(0)) + c.Check(pkg.Disk, gc.FitsTypeOf, int(0)) + c.Check(pkg.Swap, gc.FitsTypeOf, int(0)) + c.Check(pkg.VCPUs, gc.FitsTypeOf, int(0)) + c.Check(pkg.Default, gc.FitsTypeOf, bool(false)) + c.Check(pkg.Id, gc.FitsTypeOf, string("")) + c.Check(pkg.Version, gc.FitsTypeOf, string("")) + c.Check(pkg.Description, gc.FitsTypeOf, string("")) + c.Check(pkg.Group, gc.FitsTypeOf, string("")) + } +} + +func (s *LocalTests) TestListPackagesWithFilter(c *gc.C) { + filter := cloudapi.NewFilter() + filter.Set("memory", "1024") + pkgs, err := s.testClient.ListPackages(filter) + c.Assert(err, gc.IsNil) + c.Assert(pkgs, gc.NotNil) + for _, pkg := range pkgs { + c.Check(pkg.Name, gc.FitsTypeOf, string("")) + c.Check(pkg.Memory, gc.Equals, 1024) + c.Check(pkg.Disk, gc.FitsTypeOf, int(0)) + c.Check(pkg.Swap, gc.FitsTypeOf, int(0)) + c.Check(pkg.VCPUs, gc.FitsTypeOf, int(0)) + c.Check(pkg.Default, gc.FitsTypeOf, bool(false)) + c.Check(pkg.Id, gc.FitsTypeOf, string("")) + c.Check(pkg.Version, gc.FitsTypeOf, string("")) + c.Check(pkg.Description, gc.FitsTypeOf, string("")) + c.Check(pkg.Group, gc.FitsTypeOf, string("")) + } +} + +func (s *LocalTests) TestGetPackageFromName(c *gc.C) { + key, err := s.testClient.GetPackage(localPackageName) + c.Assert(err, gc.IsNil) + c.Assert(key, gc.NotNil) + c.Assert(key, gc.DeepEquals, &cloudapi.Package{ + Name: "Small", + Memory: 1024, + Disk: 16384, + Swap: 2048, + VCPUs: 1, + Default: true, + Id: "11223344-1212-abab-3434-aabbccddeeff", + Version: "1.0.2", + }) +} + +func (s *LocalTests) TestGetPackageFromId(c *gc.C) { + key, err := s.testClient.GetPackage(localPackageId) + c.Assert(err, gc.IsNil) + c.Assert(key, gc.NotNil) + c.Assert(key, gc.DeepEquals, &cloudapi.Package{ + Name: "Small", + Memory: 1024, + Disk: 16384, + Swap: 2048, + VCPUs: 1, + Default: true, + Id: "11223344-1212-abab-3434-aabbccddeeff", + Version: "1.0.2", + }) +} + +// Images API +func (s *LocalTests) TestListImages(c *gc.C) { + imgs, err := s.testClient.ListImages(nil) + c.Assert(err, gc.IsNil) + c.Assert(imgs, gc.NotNil) + for _, img := range imgs { + c.Check(img.Id, gc.FitsTypeOf, string("")) + c.Check(img.Name, gc.FitsTypeOf, string("")) + c.Check(img.OS, gc.FitsTypeOf, string("")) + c.Check(img.Version, gc.FitsTypeOf, string("")) + c.Check(img.Type, gc.FitsTypeOf, string("")) + c.Check(img.Description, gc.FitsTypeOf, string("")) + c.Check(img.Requirements, gc.FitsTypeOf, map[string]interface{}{"key": "value"}) + c.Check(img.Homepage, gc.FitsTypeOf, string("")) + c.Check(img.PublishedAt, gc.FitsTypeOf, string("")) + c.Check(img.Public, gc.FitsTypeOf, string("")) + c.Check(img.State, gc.FitsTypeOf, string("")) + c.Check(img.Tags, gc.FitsTypeOf, map[string]string{"key": "value"}) + c.Check(img.EULA, gc.FitsTypeOf, string("")) + c.Check(img.ACL, gc.FitsTypeOf, []string{"", ""}) + } +} + +func (s *LocalTests) TestListImagesWithFilter(c *gc.C) { + filter := cloudapi.NewFilter() + filter.Set("os", "smartos") + imgs, err := s.testClient.ListImages(filter) + c.Assert(err, gc.IsNil) + c.Assert(imgs, gc.NotNil) + for _, img := range imgs { + c.Check(img.Id, gc.FitsTypeOf, string("")) + c.Check(img.Name, gc.FitsTypeOf, string("")) + c.Check(img.OS, gc.Equals, "smartos") + c.Check(img.Version, gc.FitsTypeOf, string("")) + c.Check(img.Type, gc.FitsTypeOf, string("")) + c.Check(img.Description, gc.FitsTypeOf, string("")) + c.Check(img.Requirements, gc.FitsTypeOf, map[string]interface{}{"key": "value"}) + c.Check(img.Homepage, gc.FitsTypeOf, string("")) + c.Check(img.PublishedAt, gc.FitsTypeOf, string("")) + c.Check(img.Public, gc.FitsTypeOf, string("")) + c.Check(img.State, gc.FitsTypeOf, string("")) + c.Check(img.Tags, gc.FitsTypeOf, map[string]string{"key": "value"}) + c.Check(img.EULA, gc.FitsTypeOf, string("")) + c.Check(img.ACL, gc.FitsTypeOf, []string{"", ""}) + } +} + +// TODO Add test for deleteImage, exportImage and CreateMachineFormIMage + +func (s *LocalTests) TestGetImage(c *gc.C) { + img, err := s.testClient.GetImage(localImageId) + c.Assert(err, gc.IsNil) + c.Assert(img, gc.NotNil) + c.Assert(img, gc.DeepEquals, &cloudapi.Image{ + Id: "12345678-a1a1-b2b2-c3c3-098765432100", + Name: "SmartOS Std", + OS: "smartos", + Version: "13.3.1", + Type: "smartmachine", + Description: "Test SmartOS image (32 bit)", + Homepage: "http://test.joyent.com/Standard_Instance", + PublishedAt: "2014-01-08T17:42:31Z", + Public: "true", + State: "active", + }) +} + +// Tests for Machine API +func (s *LocalTests) TestCreateMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + c.Assert(testMachine.Type, gc.Equals, "smartmachine") + c.Assert(testMachine.Memory, gc.Equals, 1024) + c.Assert(testMachine.Disk, gc.Equals, 16384) + c.Assert(testMachine.Package, gc.Equals, localPackageName) + c.Assert(testMachine.Image, gc.Equals, localImageId) +} + +func (s *LocalTests) TestListMachines(c *gc.C) { + s.listMachines(c, nil) +} + +func (s *LocalTests) TestListMachinesWithFilter(c *gc.C) { + filter := cloudapi.NewFilter() + filter.Set("memory", "1024") + + s.listMachines(c, filter) +} + +/*func (s *LocalTests) TestCountMachines(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + count, err := s.testClient.CountMachines() + c.Assert(err, gc.IsNil) + c.Assert(count >= 1, gc.Equals, true) +}*/ + +func (s *LocalTests) TestGetMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + machine, err := s.testClient.GetMachine(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(machine, gc.NotNil) + c.Assert(machine.Equals(*testMachine), gc.Equals, true) +} + +func (s *LocalTests) TestStopMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.StopMachine(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LocalTests) TestStartMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.StopMachine(testMachine.Id) + c.Assert(err, gc.IsNil) + + // wait for machine to be stopped + for !s.pollMachineState(c, testMachine.Id, "stopped") { + time.Sleep(1 * time.Second) + } + + err = s.testClient.StartMachine(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LocalTests) TestRebootMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.RebootMachine(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LocalTests) TestRenameMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.RenameMachine(testMachine.Id, "test-machine-renamed") + c.Assert(err, gc.IsNil) + + renamed, err := s.testClient.GetMachine(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(renamed.Name, gc.Equals, "test-machine-renamed") +} + +func (s *LocalTests) TestResizeMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.ResizeMachine(testMachine.Id, "Medium") + c.Assert(err, gc.IsNil) + + resized, err := s.testClient.GetMachine(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(resized.Package, gc.Equals, "Medium") +} + +func (s *LocalTests) TestListMachinesFirewallRules(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + fwRules, err := s.testClient.ListMachineFirewallRules(testMachine.Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRules, gc.NotNil) +} + +func (s *LocalTests) TestEnableFirewallMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.EnableFirewallMachine(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LocalTests) TestDisableFirewallMachine(c *gc.C) { + testMachine := s.createMachine(c) + defer s.deleteMachine(c, testMachine.Id) + + err := s.testClient.DisableFirewallMachine(testMachine.Id) + c.Assert(err, gc.IsNil) +} + +func (s *LocalTests) TestDeleteMachine(c *gc.C) { + testMachine := s.createMachine(c) + + s.deleteMachine(c, testMachine.Id) +} + +// FirewallRules API +func (s *LocalTests) TestCreateFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + // cleanup + s.deleteFwRule(c, testFwRule.Id) +} + +func (s *LocalTests) TestListFirewallRules(c *gc.C) { + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + rules, err := s.testClient.ListFirewallRules() + c.Assert(err, gc.IsNil) + c.Assert(rules, gc.NotNil) +} + +func (s *LocalTests) TestGetFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + fwRule, err := s.testClient.GetFirewallRule(testFwRule.Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) + c.Assert((*fwRule), gc.DeepEquals, (*testFwRule)) +} + +func (s *LocalTests) TestUpdateFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + fwRule, err := s.testClient.UpdateFirewallRule(testFwRule.Id, cloudapi.CreateFwRuleOpts{Rule: testUpdatedFwRule}) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) + c.Assert(fwRule.Rule, gc.Equals, testUpdatedFwRule) +} + +func (s *LocalTests) TestEnableFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + fwRule, err := s.testClient.EnableFirewallRule((*testFwRule).Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) +} + +func (s *LocalTests) TestDisableFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + fwRule, err := s.testClient.DisableFirewallRule((*testFwRule).Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) +} + +func (s *LocalTests) TestDeleteFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + s.deleteFwRule(c, testFwRule.Id) +} + +// Networks API +func (s *LocalTests) TestListNetworks(c *gc.C) { + nets, err := s.testClient.ListNetworks() + c.Assert(err, gc.IsNil) + c.Assert(nets, gc.NotNil) +} + +func (s *LocalTests) TestGetNetwork(c *gc.C) { + net, err := s.testClient.GetNetwork(localNetworkId) + c.Assert(err, gc.IsNil) + c.Assert(net, gc.NotNil) + c.Assert(net, gc.DeepEquals, &cloudapi.Network{ + Id: localNetworkId, + Name: "Test-Joyent-Public", + Public: true, + Description: "", + }) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/COPYING juju-core-1.18.0/src/github.com/joyent/gosdc/COPYING --- juju-core-1.17.7/src/github.com/joyent/gosdc/COPYING 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/COPYING 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/COPYING.LESSER juju-core-1.18.0/src/github.com/joyent/gosdc/COPYING.LESSER --- juju-core-1.17.7/src/github.com/joyent/gosdc/COPYING.LESSER 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/COPYING.LESSER 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/.gitignore juju-core-1.18.0/src/github.com/joyent/gosdc/.gitignore --- juju-core-1.17.7/src/github.com/joyent/gosdc/.gitignore 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/.gitignore 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,26 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe + +# IntelliJ files +.idea +*.iml \ No newline at end of file diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/gosdc.go juju-core-1.18.0/src/github.com/joyent/gosdc/gosdc.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/gosdc.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/gosdc.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,15 @@ +/* +The gosdc package enables Go programs to interact with the Joyent CloudAPI. + +The gosdc package is structured as follow: + + - gosdc/cloudapi. This package interacts with the Cloud API (http://apidocs.joyent.com/cloudapi/). + - gosdc/localservices. This package provides local services to be used for testing. + +Licensed under LGPL v3. + +Copyright (c) 2013 Joyent Inc. +Written by Daniele Stroppa + +*/ +package gosdc diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/gosdc_test.go juju-core-1.18.0/src/github.com/joyent/gosdc/gosdc_test.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/gosdc_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/gosdc_test.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,24 @@ +// +// gosdc - Go library to interact with the Joyent CloudAPI +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package gosdc + +import ( + gc "launchpad.net/gocheck" + "testing" +) + +func Test(t *testing.T) { + gc.TestingT(t) +} + +type GoSdcTestSuite struct { +} + +var _ = gc.Suite(&GoSdcTestSuite{}) diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/LICENSE juju-core-1.18.0/src/github.com/joyent/gosdc/LICENSE --- juju-core-1.17.7/src/github.com/joyent/gosdc/LICENSE 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/LICENSE 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,15 @@ +GoSdc - Go Library for the Joyent Public Cloud + +Copyright (c) 2013, Joyent Inc. + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU Lesser General Public License as published by the Free +Software Foundation, either version 3 of the License, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + +See both COPYING and COPYING.LESSER for the full terms of the GNU Lesser +General Public License. diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/cloudapi/service.go juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/cloudapi/service.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/cloudapi/service.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/cloudapi/service.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,778 @@ +// +// gosdc - Go library to interact with the Joyent CloudAPI +// +// CloudAPI double testing service - internal direct API implementation +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package cloudapi + +import ( + "fmt" + "math/rand" + "net/url" + "strconv" + "strings" + "time" + + "github.com/joyent/gosdc/cloudapi" + "github.com/joyent/gosdc/localservices" +) + +var ( + separator = "/" + packagesFilters = []string{"name", "memory", "disk", "swap", "version", "vcpus", "group"} + imagesFilters = []string{"name", "os", "version", "public", "state", "owner", "type"} + machinesFilters = []string{"type", "name", "image", "state", "memory", "tombstone", "limit", "offset", "credentials"} +) + +type CloudAPI struct { + localservices.ServiceInstance + keys []cloudapi.Key + packages []cloudapi.Package + images []cloudapi.Image + machines []*cloudapi.Machine + machineFw map[string]bool + snapshots map[string][]cloudapi.Snapshot + firewallRules []*cloudapi.FirewallRule + networks []cloudapi.Network +} + +func New(serviceURL, userAccount string) *CloudAPI { + URL, err := url.Parse(serviceURL) + if err != nil { + panic(err) + } + hostname := URL.Host + if !strings.HasSuffix(hostname, separator) { + hostname += separator + } + + keys := make([]cloudapi.Key, 0) + machines := make([]*cloudapi.Machine, 0) + machineFw := make(map[string]bool) + snapshots := make(map[string][]cloudapi.Snapshot) + firewallRules := make([]*cloudapi.FirewallRule, 0) + + cloudapiService := &CloudAPI{ + keys: keys, + packages: initPackages(), + images: initImages(), + machines: machines, + machineFw: machineFw, + snapshots: snapshots, + firewallRules: firewallRules, + networks: []cloudapi.Network{ + {Id: "123abc4d-0011-aabb-2233-ccdd4455", Name: "Test-Joyent-Public", Public: true}, + {Id: "456def0a-33ff-7f8e-9a0b-33bb44cc", Name: "Test-Joyent-Private", Public: false}, + }, + ServiceInstance: localservices.ServiceInstance{ + Scheme: URL.Scheme, + Hostname: hostname, + UserAccount: userAccount, + }, + } + + return cloudapiService +} + +func initPackages() []cloudapi.Package { + return []cloudapi.Package{ + { + Name: "Micro", + Memory: 512, + Disk: 8192, + Swap: 1024, + VCPUs: 1, + Default: false, + Id: "12345678-aaaa-bbbb-cccc-000000000000", + Version: "1.0.0", + }, + { + Name: "Small", + Memory: 1024, + Disk: 16384, + Swap: 2048, + VCPUs: 1, + Default: true, + Id: "11223344-1212-abab-3434-aabbccddeeff", + Version: "1.0.2", + }, + { + Name: "Medium", + Memory: 2048, + Disk: 32768, + Swap: 4096, + VCPUs: 2, + Default: false, + Id: "aabbccdd-abcd-abcd-abcd-112233445566", + Version: "1.0.4", + }, + { + Name: "Large", + Memory: 4096, + Disk: 65536, + Swap: 16384, + VCPUs: 4, + Default: false, + Id: "00998877-dddd-eeee-ffff-111111111111", + Version: "1.0.1", + }, + } +} + +func initImages() []cloudapi.Image { + return []cloudapi.Image{ + { + Id: "12345678-a1a1-b2b2-c3c3-098765432100", + Name: "SmartOS Std", + OS: "smartos", + Version: "13.3.1", + Type: "smartmachine", + Description: "Test SmartOS image (32 bit)", + Homepage: "http://test.joyent.com/Standard_Instance", + PublishedAt: "2014-01-08T17:42:31Z", + Public: "true", + State: "active", + }, + { + Id: "12345678-b1b1-a4a4-d8d8-111111999999", + Name: "standard32", + OS: "smartos", + Version: "13.3.1", + Type: "smartmachine", + Description: "Test SmartOS image (64 bit)", + Homepage: "http://test.joyent.com/Standard_Instance", + PublishedAt: "2014-01-08T17:43:16Z", + Public: "true", + State: "active", + }, + { + Id: "a1b2c3d4-0011-2233-4455-0f1e2d3c4b5a", + Name: "centos6.4", + OS: "linux", + Version: "2.4.1", + Type: "virtualmachine", + Description: "Test CentOS 6.4 image (64 bit)", + PublishedAt: "2014-01-02T10:58:31Z", + Public: "true", + State: "active", + }, + { + Id: "11223344-0a0a-ff99-11bb-0a1b2c3d4e5f", + Name: "ubuntu12.04", + OS: "linux", + Version: "2.3.1", + Type: "virtualmachine", + Description: "Test Ubuntu 12.04 image (64 bit)", + PublishedAt: "2014-01-20T16:12:31Z", + Public: "true", + State: "active", + }, + { + Id: "11223344-0a0a-ee88-22ab-00aa11bb22cc", + Name: "ubuntu12.10", + OS: "linux", + Version: "2.3.2", + Type: "virtualmachine", + Description: "Test Ubuntu 12.10 image (64 bit)", + PublishedAt: "2014-01-20T16:12:31Z", + Public: "true", + State: "active", + }, + { + Id: "11223344-0a0a-dd77-33cd-abcd1234e5f6", + Name: "ubuntu13.04", + OS: "linux", + Version: "2.2.8", + Type: "virtualmachine", + Description: "Test Ubuntu 13.04 image (64 bit)", + PublishedAt: "2014-01-20T16:12:31Z", + Public: "true", + State: "active", + }, + } +} + +func generatePublicIPAddress() string { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + return fmt.Sprintf("32.151.%d.%d", r.Intn(255), r.Intn(255)) +} + +func generatePrivateIPAddress() string { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + return fmt.Sprintf("10.201.%d.%d", r.Intn(255), r.Intn(255)) +} + +func contains(list []string, elem string) bool { + for _, t := range list { + if t == elem { + return true + } + } + return false +} + +// Keys APIs +func (c *CloudAPI) ListKeys() ([]cloudapi.Key, error) { + if err := c.ProcessFunctionHook(c); err != nil { + return nil, err + } + + return c.keys, nil +} + +func (c *CloudAPI) GetKey(keyName string) (*cloudapi.Key, error) { + if err := c.ProcessFunctionHook(c, keyName); err != nil { + return nil, err + } + + for _, key := range c.keys { + if key.Name == keyName { + return &key, nil + } + } + + return nil, fmt.Errorf("Key %s not found", keyName) +} + +func (c *CloudAPI) CreateKey(keyName, key string) (*cloudapi.Key, error) { + if err := c.ProcessFunctionHook(c, keyName, key); err != nil { + return nil, err + } + + // check if key already exists or keyName already in use + for _, k := range c.keys { + if k.Name == keyName { + return nil, fmt.Errorf("Key name %s already in use", keyName) + } + if k.Key == key { + return nil, fmt.Errorf("Key %s already exists", key) + } + } + + newKey := cloudapi.Key{Name: keyName, Fingerprint: "", Key: key} + c.keys = append(c.keys, newKey) + + return &newKey, nil +} + +func (c *CloudAPI) DeleteKey(keyName string) error { + if err := c.ProcessFunctionHook(c, keyName); err != nil { + return err + } + + for i, key := range c.keys { + if key.Name == keyName { + c.keys = append(c.keys[:i], c.keys[i+1:]...) + return nil + } + } + + return fmt.Errorf("Key %s not found", keyName) +} + +// Packages APIs +func (c *CloudAPI) ListPackages(filters map[string]string) ([]cloudapi.Package, error) { + if err := c.ProcessFunctionHook(c, filters); err != nil { + return nil, err + } + + availablePackages := c.packages + + if filters != nil { + for k, f := range filters { + // check if valid filter + if contains(packagesFilters, k) { + pkgs := []cloudapi.Package{} + // filter from availablePackages and add to pkgs + for _, p := range availablePackages { + if k == "name" && p.Name == f { + pkgs = append(pkgs, p) + } else if k == "memory" { + i, err := strconv.Atoi(f) + if err == nil && p.Memory == i { + pkgs = append(pkgs, p) + } + } else if k == "disk" { + i, err := strconv.Atoi(f) + if err == nil && p.Disk == i { + pkgs = append(pkgs, p) + } + } else if k == "swap" { + i, err := strconv.Atoi(f) + if err == nil && p.Swap == i { + pkgs = append(pkgs, p) + } + } else if k == "version" && p.Version == f { + pkgs = append(pkgs, p) + } else if k == "vcpus" { + i, err := strconv.Atoi(f) + if err == nil && p.VCPUs == i { + pkgs = append(pkgs, p) + } + } else if k == "group" && p.Group == f { + pkgs = append(pkgs, p) + } + } + availablePackages = pkgs + } + } + } + + return availablePackages, nil +} + +func (c *CloudAPI) GetPackage(packageName string) (*cloudapi.Package, error) { + if err := c.ProcessFunctionHook(c, packageName); err != nil { + return nil, err + } + + for _, pkg := range c.packages { + if pkg.Name == packageName { + return &pkg, nil + } + if pkg.Id == packageName { + return &pkg, nil + } + } + + return nil, fmt.Errorf("Package %s not found", packageName) +} + +// Images APIs +func (c *CloudAPI) ListImages(filters map[string]string) ([]cloudapi.Image, error) { + if err := c.ProcessFunctionHook(c, filters); err != nil { + return nil, err + } + + availableImages := c.images + + if filters != nil { + for k, f := range filters { + // check if valid filter + if contains(imagesFilters, k) { + imgs := []cloudapi.Image{} + // filter from availableImages and add to imgs + for _, i := range availableImages { + if k == "name" && i.Name == f { + imgs = append(imgs, i) + } else if k == "os" && i.OS == f { + imgs = append(imgs, i) + } else if k == "version" && i.Version == f { + imgs = append(imgs, i) + } else if k == "public" && i.Public == f { + imgs = append(imgs, i) + } else if k == "state" && i.State == f { + imgs = append(imgs, i) + } else if k == "owner" && i.Owner == f { + imgs = append(imgs, i) + } else if k == "type" && i.Type == f { + imgs = append(imgs, i) + } + } + availableImages = imgs + } + } + } + + return availableImages, nil +} + +func (c *CloudAPI) GetImage(imageId string) (*cloudapi.Image, error) { + if err := c.ProcessFunctionHook(c, imageId); err != nil { + return nil, err + } + + for _, image := range c.images { + if image.Id == imageId { + return &image, nil + } + } + + return nil, fmt.Errorf("Image %s not found", imageId) +} + +// Machine APIs +func (c *CloudAPI) ListMachines(filters map[string]string) ([]*cloudapi.Machine, error) { + if err := c.ProcessFunctionHook(c, filters); err != nil { + return nil, err + } + + availableMachines := c.machines + + if filters != nil { + for k, f := range filters { + // check if valid filter + if contains(machinesFilters, k) { + machines := []*cloudapi.Machine{} + // filter from availableMachines and add to machines + for _, m := range availableMachines { + if k == "name" && m.Name == f { + machines = append(machines, m) + } else if k == "type" && m.Type == f { + machines = append(machines, m) + } else if k == "state" && m.State == f { + machines = append(machines, m) + } else if k == "image" && m.Image == f { + machines = append(machines, m) + } else if k == "memory" { + i, err := strconv.Atoi(f) + if err == nil && m.Memory == i { + machines = append(machines, m) + } + } else if strings.HasPrefix(k, "tags.") { + for t, v := range m.Tags { + if t == k[strings.Index(k, ".")+1:] && v == f { + machines = append(machines, m) + } + } + } + } + availableMachines = machines + } + } + } + + return availableMachines, nil +} + +func (c *CloudAPI) CountMachines() (int, error) { + if err := c.ProcessFunctionHook(c); err != nil { + return 0, err + } + + return len(c.machines), nil +} + +func (c *CloudAPI) GetMachine(machineId string) (*cloudapi.Machine, error) { + if err := c.ProcessFunctionHook(c, machineId); err != nil { + return nil, err + } + + for _, machine := range c.machines { + if machine.Id == machineId { + return machine, nil + } + } + + return nil, fmt.Errorf("Machine %s not found", machineId) +} + +func (c *CloudAPI) CreateMachine(name, pkg, image string, metadata, tags map[string]string) (*cloudapi.Machine, error) { + if err := c.ProcessFunctionHook(c, name, pkg, image); err != nil { + return nil, err + } + + machineId, err := localservices.NewUUID() + if err != nil { + return nil, err + } + + mPkg, err := c.GetPackage(pkg) + if err != nil { + return nil, err + } + + mImg, err := c.GetImage(image) + if err != nil { + return nil, err + } + + publicIP := generatePublicIPAddress() + + newMachine := &cloudapi.Machine{ + Id: machineId, + Name: name, + Type: mImg.Type, + State: "running", + Memory: mPkg.Memory, + Disk: mPkg.Disk, + IPs: []string{publicIP, generatePrivateIPAddress()}, + Created: time.Now().Format("2013-11-26T19:47:13.448Z"), + Package: pkg, + Image: image, + Metadata: metadata, + Tags: tags, + PrimaryIP: publicIP, + } + c.machines = append(c.machines, newMachine) + + return newMachine, nil +} + +func (c *CloudAPI) StopMachine(machineId string) error { + if err := c.ProcessFunctionHook(c, machineId); err != nil { + return err + } + + for _, machine := range c.machines { + if machine.Id == machineId { + machine.State = "stopped" + machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z") + return nil + } + } + + return fmt.Errorf("Machine %s not found", machineId) +} + +func (c *CloudAPI) StartMachine(machineId string) error { + if err := c.ProcessFunctionHook(c, machineId); err != nil { + return err + } + + for _, machine := range c.machines { + if machine.Id == machineId { + machine.State = "running" + machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z") + return nil + } + } + + return fmt.Errorf("Machine %s not found", machineId) +} + +func (c *CloudAPI) RebootMachine(machineId string) error { + if err := c.ProcessFunctionHook(c, machineId); err != nil { + return err + } + + for _, machine := range c.machines { + if machine.Id == machineId { + machine.State = "running" + machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z") + return nil + } + } + + return fmt.Errorf("Machine %s not found", machineId) +} + +func (c *CloudAPI) ResizeMachine(machineId, packageName string) error { + if err := c.ProcessFunctionHook(c, machineId, packageName); err != nil { + return err + } + + mPkg, err := c.GetPackage(packageName) + if err != nil { + return err + } + + for _, machine := range c.machines { + if machine.Id == machineId { + machine.Package = packageName + machine.Memory = mPkg.Memory + machine.Disk = mPkg.Disk + machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z") + return nil + } + } + + return fmt.Errorf("Machine %s not found", machineId) +} + +func (c *CloudAPI) RenameMachine(machineId, newName string) error { + if err := c.ProcessFunctionHook(c, machineId, newName); err != nil { + return err + } + + for _, machine := range c.machines { + if machine.Id == machineId { + machine.Name = newName + machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z") + return nil + } + } + + return fmt.Errorf("Machine %s not found", machineId) +} + +func (c *CloudAPI) ListMachineFirewallRules(machineId string) ([]*cloudapi.FirewallRule, error) { + if err := c.ProcessFunctionHook(c, machineId); err != nil { + return nil, err + } + + fwRules := []*cloudapi.FirewallRule{} + for _, r := range c.firewallRules { + vm := "vm " + machineId + if strings.Contains(r.Rule, vm) { + fwRules = append(fwRules, r) + } + } + + return fwRules, nil +} + +func (c *CloudAPI) EnableFirewallMachine(machineId string) error { + if err := c.ProcessFunctionHook(c, machineId); err != nil { + return err + } + + c.machineFw[machineId] = true + + return nil +} + +func (c *CloudAPI) DisableFirewallMachine(machineId string) error { + if err := c.ProcessFunctionHook(c, machineId); err != nil { + return err + } + + c.machineFw[machineId] = false + + return nil +} + +func (c *CloudAPI) DeleteMachine(machineId string) error { + if err := c.ProcessFunctionHook(c, machineId); err != nil { + return err + } + + for i, machine := range c.machines { + if machine.Id == machineId { + if machine.State == "stopped" { + c.machines = append(c.machines[:i], c.machines[i+1:]...) + return nil + } else { + return fmt.Errorf("Cannot Delete machine %s, machine is not stopped.", machineId) + } + } + } + + return fmt.Errorf("Machine %s not found", machineId) +} + +// FirewallRule APIs +func (c *CloudAPI) ListFirewallRules() ([]*cloudapi.FirewallRule, error) { + if err := c.ProcessFunctionHook(c); err != nil { + return nil, err + } + + return c.firewallRules, nil +} + +func (c *CloudAPI) GetFirewallRule(fwRuleId string) (*cloudapi.FirewallRule, error) { + if err := c.ProcessFunctionHook(c, fwRuleId); err != nil { + return nil, err + } + + for _, r := range c.firewallRules { + if strings.EqualFold(r.Id, fwRuleId) { + return r, nil + } + } + + return nil, fmt.Errorf("Firewall rule %s not found", fwRuleId) +} + +func (c *CloudAPI) CreateFirewallRule(rule string, enabled bool) (*cloudapi.FirewallRule, error) { + if err := c.ProcessFunctionHook(c, rule, enabled); err != nil { + return nil, err + } + + fwRuleId, err := localservices.NewUUID() + if err != nil { + return nil, fmt.Errorf("Error creating firewall rule: %q", err) + } + + fwRule := &cloudapi.FirewallRule{Id: fwRuleId, Rule: rule, Enabled: enabled} + c.firewallRules = append(c.firewallRules, fwRule) + + return fwRule, nil +} + +func (c *CloudAPI) UpdateFirewallRule(fwRuleId, rule string, enabled bool) (*cloudapi.FirewallRule, error) { + if err := c.ProcessFunctionHook(c, fwRuleId, rule, enabled); err != nil { + return nil, err + } + + for _, r := range c.firewallRules { + if strings.EqualFold(r.Id, fwRuleId) { + r.Rule = rule + r.Enabled = enabled + return r, nil + } + } + + return nil, fmt.Errorf("Firewall rule %s not found", fwRuleId) +} + +func (c *CloudAPI) EnableFirewallRule(fwRuleId string) (*cloudapi.FirewallRule, error) { + if err := c.ProcessFunctionHook(c, fwRuleId); err != nil { + return nil, err + } + + for _, r := range c.firewallRules { + if strings.EqualFold(r.Id, fwRuleId) { + r.Enabled = true + return r, nil + } + } + + return nil, fmt.Errorf("Firewall rule %s not found", fwRuleId) +} + +func (c *CloudAPI) DisableFirewallRule(fwRuleId string) (*cloudapi.FirewallRule, error) { + if err := c.ProcessFunctionHook(c, fwRuleId); err != nil { + return nil, err + } + + for _, r := range c.firewallRules { + if strings.EqualFold(r.Id, fwRuleId) { + r.Enabled = false + return r, nil + } + } + + return nil, fmt.Errorf("Firewall rule %s not found", fwRuleId) +} + +func (c *CloudAPI) DeleteFirewallRule(fwRuleId string) error { + if err := c.ProcessFunctionHook(c, fwRuleId); err != nil { + return err + } + + for i, r := range c.firewallRules { + if strings.EqualFold(r.Id, fwRuleId) { + c.firewallRules = append(c.firewallRules[:i], c.firewallRules[i+1:]...) + return nil + } + } + + return fmt.Errorf("Firewall rule %s not found", fwRuleId) +} + +func (c *CloudAPI) ListFirewallRuleMachines(fwRuleId string) ([]*cloudapi.Machine, error) { + if err := c.ProcessFunctionHook(c, fwRuleId); err != nil { + return nil, err + } + + return c.machines, nil +} + +// Networks API +func (c *CloudAPI) ListNetworks() ([]cloudapi.Network, error) { + if err := c.ProcessFunctionHook(c); err != nil { + return nil, err + } + + return c.networks, nil +} + +func (c *CloudAPI) GetNetwork(networkId string) (*cloudapi.Network, error) { + if err := c.ProcessFunctionHook(c, networkId); err != nil { + return nil, err + } + + for _, n := range c.networks { + if strings.EqualFold(n.Id, networkId) { + return &n, nil + } + } + + return nil, fmt.Errorf("Network %s not found", networkId) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/cloudapi/service_http.go juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/cloudapi/service_http.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/cloudapi/service_http.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/cloudapi/service_http.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,659 @@ +// +// gosdc - Go library to interact with the Joyent CloudAPI +// +// CloudAPI double testing service - HTTP API implementation +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package cloudapi + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + "strings" + + "github.com/joyent/gosdc/cloudapi" +) + +// ErrorResponse defines a single HTTP error response. +type ErrorResponse struct { + Code int + Body string + contentType string + errorText string + headers map[string]string + cloudapi *CloudAPI +} + +var ( + ErrNotAllowed = &ErrorResponse{ + http.StatusMethodNotAllowed, + "Method is not allowed", + "text/plain; charset=UTF-8", + "MethodNotAllowedError", + nil, + nil, + } + ErrNotFound = &ErrorResponse{ + http.StatusNotFound, + "Resource Not Found", + "text/plain; charset=UTF-8", + "NotFoundError", + nil, + nil, + } + ErrBadRequest = &ErrorResponse{ + http.StatusBadRequest, + "Malformed request url", + "text/plain; charset=UTF-8", + "BadRequestError", + nil, + nil, + } +) + +func (e *ErrorResponse) Error() string { + return e.errorText +} + +func (e *ErrorResponse) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if e.contentType != "" { + w.Header().Set("Content-Type", e.contentType) + } + body := e.Body + if e.headers != nil { + for h, v := range e.headers { + w.Header().Set(h, v) + } + } + // workaround for https://code.google.com/p/go/issues/detail?id=4454 + w.Header().Set("Content-Length", strconv.Itoa(len(body))) + if e.Code != 0 { + w.WriteHeader(e.Code) + } + if len(body) > 0 { + w.Write([]byte(body)) + } +} + +type cloudapiHandler struct { + cloudapi *CloudAPI + method func(m *CloudAPI, w http.ResponseWriter, r *http.Request) error +} + +func (h *cloudapiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + // handle trailing slash in the path + if strings.HasSuffix(path, "/") && path != "/" { + ErrNotFound.ServeHTTP(w, r) + return + } + err := h.method(h.cloudapi, w, r) + if err == nil { + return + } + var resp http.Handler + resp, _ = err.(http.Handler) + if resp == nil { + resp = &ErrorResponse{ + http.StatusInternalServerError, + `{"internalServerError":{"message":"Unkown Error",code:500}}`, + "application/json", + err.Error(), + nil, + h.cloudapi, + } + } + resp.ServeHTTP(w, r) +} + +func writeResponse(w http.ResponseWriter, code int, body []byte) { + // workaround for https://code.google.com/p/go/issues/detail?id=4454 + w.Header().Set("Content-Length", strconv.Itoa(len(body))) + w.WriteHeader(code) + w.Write(body) +} + +// sendJSON sends the specified response serialized as JSON. +func sendJSON(code int, resp interface{}, w http.ResponseWriter, r *http.Request) error { + data, err := json.Marshal(resp) + if err != nil { + return err + } + writeResponse(w, code, data) + return nil +} + +func processFilter(rawQuery string) map[string]string { + var filters map[string]string + if rawQuery != "" { + filters = make(map[string]string) + for _, filter := range strings.Split(rawQuery, "&") { + filters[filter[:strings.Index(filter, "=")]] = filter[strings.Index(filter, "=")+1:] + } + } + + return filters +} + +func (cloudapi *CloudAPI) handler(method func(m *CloudAPI, w http.ResponseWriter, r *http.Request) error) http.Handler { + return &cloudapiHandler{cloudapi, method} +} + +// handleKeys handles the keys HTTP API. +func (c *CloudAPI) handleKeys(w http.ResponseWriter, r *http.Request) error { + prefix := fmt.Sprintf("/%s/keys/", c.ServiceInstance.UserAccount) + keyName := strings.TrimPrefix(r.URL.Path, prefix) + switch r.Method { + case "GET": + if strings.HasSuffix(r.URL.Path, "keys") { + // ListKeys + keys, err := c.ListKeys() + if err != nil { + return err + } + if keys == nil { + keys = []cloudapi.Key{} + } + resp := keys + return sendJSON(http.StatusOK, resp, w, r) + } else { + // GetKey + key, err := c.GetKey(keyName) + if err != nil { + return err + } + if key == nil { + key = &cloudapi.Key{} + } + resp := key + return sendJSON(http.StatusOK, resp, w, r) + } + case "POST": + if strings.HasSuffix(r.URL.Path, "keys") { + // CreateKey + var ( + name string + key string + ) + opts := &cloudapi.CreateKeyOpts{} + body, errB := ioutil.ReadAll(r.Body) + if errB != nil { + return errB + } + if len(body) > 0 { + if errJ := json.Unmarshal(body, opts); errJ != nil { + return errJ + } + name = opts.Name + key = opts.Key + } + k, err := c.CreateKey(name, key) + if err != nil { + return err + } + if k == nil { + k = &cloudapi.Key{} + } + resp := k + return sendJSON(http.StatusCreated, resp, w, r) + } else { + return ErrNotAllowed + } + case "PUT": + return ErrNotAllowed + case "DELETE": + if strings.HasSuffix(r.URL.Path, "keys") { + return ErrNotAllowed + } else { + // DeleteKey + err := c.DeleteKey(keyName) + if err != nil { + return err + } + return sendJSON(http.StatusNoContent, nil, w, r) + } + } + return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path) +} + +// handleImages handles the images HTTP API. +func (c *CloudAPI) handleImages(w http.ResponseWriter, r *http.Request) error { + prefix := fmt.Sprintf("/%s/images/", c.ServiceInstance.UserAccount) + imageId := strings.TrimPrefix(r.URL.Path, prefix) + switch r.Method { + case "GET": + if strings.HasSuffix(r.URL.Path, "images") { + // ListImages + images, err := c.ListImages(processFilter(r.URL.RawQuery)) + if err != nil { + return err + } + if images == nil { + images = []cloudapi.Image{} + } + resp := images + return sendJSON(http.StatusOK, resp, w, r) + } else { + // GetImage + image, err := c.GetImage(imageId) + if err != nil { + return err + } + if image == nil { + image = &cloudapi.Image{} + } + resp := image + return sendJSON(http.StatusOK, resp, w, r) + } + case "POST": + if strings.HasSuffix(r.URL.Path, "images") { + // CreateImageFromMachine + return ErrNotFound + } else { + return ErrNotAllowed + } + case "PUT": + return ErrNotAllowed + case "DELETE": + /*if strings.HasSuffix(r.URL.Path, "images") { + return ErrNotAllowed + } else { + err := c.DeleteImage(imageId) + if err != nil { + return err + } + return sendJSON(http.StatusNoContent, nil, w, r) + }*/ + return ErrNotAllowed + } + return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path) +} + +// handlePackages handles the packages HTTP API. +func (c *CloudAPI) handlePackages(w http.ResponseWriter, r *http.Request) error { + prefix := fmt.Sprintf("/%s/packages/", c.ServiceInstance.UserAccount) + pkgName := strings.TrimPrefix(r.URL.Path, prefix) + switch r.Method { + case "GET": + if strings.HasSuffix(r.URL.Path, "packages") { + // ListPackages + pkgs, err := c.ListPackages(processFilter(r.URL.RawQuery)) + if err != nil { + return err + } + if pkgs == nil { + pkgs = []cloudapi.Package{} + } + resp := pkgs + return sendJSON(http.StatusOK, resp, w, r) + } else { + // GetPackage + pkg, err := c.GetPackage(pkgName) + if err != nil { + return err + } + if pkg == nil { + pkg = &cloudapi.Package{} + } + resp := pkg + return sendJSON(http.StatusOK, resp, w, r) + } + case "POST": + return ErrNotAllowed + case "PUT": + return ErrNotAllowed + case "DELETE": + return ErrNotAllowed + } + return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path) +} + +// handleMachines handles the machine HTTP API. +func (c *CloudAPI) handleMachines(w http.ResponseWriter, r *http.Request) error { + prefix := fmt.Sprintf("/%s/machines/", c.ServiceInstance.UserAccount) + machineId := strings.TrimPrefix(r.URL.Path, prefix) + switch r.Method { + case "GET": + if strings.HasSuffix(r.URL.Path, "machines") { + // ListMachines + machines, err := c.ListMachines(processFilter(r.URL.RawQuery)) + if err != nil { + return err + } + if machines == nil { + machines = []*cloudapi.Machine{} + } + resp := machines + return sendJSON(http.StatusOK, resp, w, r) + } else if strings.HasSuffix(r.URL.Path, "fwrules") { + // ListMachineFirewallRules + machineId = strings.TrimSuffix(machineId, "/fwrules") + fwRules, err := c.ListMachineFirewallRules(machineId) + if err != nil { + return err + } + if fwRules == nil { + fwRules = []*cloudapi.FirewallRule{} + } + resp := fwRules + return sendJSON(http.StatusOK, resp, w, r) + } else { + // GetMachine + machine, err := c.GetMachine(machineId) + if err != nil { + return err + } + if machine == nil { + machine = &cloudapi.Machine{} + } + resp := machine + return sendJSON(http.StatusOK, resp, w, r) + } + case "HEAD": + if strings.HasSuffix(r.URL.Path, "machines") { + // CountMachines + count, err := c.CountMachines() + if err != nil { + return err + } + resp := count + return sendJSON(http.StatusOK, resp, w, r) + } else { + return ErrNotAllowed + } + case "POST": + if strings.HasSuffix(r.URL.Path, "machines") { + // CreateMachine + var ( + name string + pkg string + image string + metadata map[string]string + tags map[string]string + ) + opts := &cloudapi.CreateMachineOpts{} + body, errB := ioutil.ReadAll(r.Body) + if errB != nil { + return errB + } + if len(body) > 0 { + if errJ := json.Unmarshal(body, opts); errJ != nil { + return errJ + } + name = opts.Name + pkg = opts.Package + image = opts.Image + metadata = opts.Metadata + tags = opts.Tags + } + machine, err := c.CreateMachine(name, pkg, image, metadata, tags) + if err != nil { + return err + } + if machine == nil { + machine = &cloudapi.Machine{} + } + resp := machine + return sendJSON(http.StatusCreated, resp, w, r) + } else if r.URL.Query().Get("action") == "stop" { + //StopMachine + err := c.StopMachine(machineId) + if err != nil { + return err + } + return sendJSON(http.StatusAccepted, nil, w, r) + } else if r.URL.Query().Get("action") == "start" { + //StartMachine + err := c.StartMachine(machineId) + if err != nil { + return err + } + return sendJSON(http.StatusAccepted, nil, w, r) + } else if r.URL.Query().Get("action") == "reboot" { + //RebootMachine + err := c.RebootMachine(machineId) + if err != nil { + return err + } + return sendJSON(http.StatusAccepted, nil, w, r) + } else if r.URL.Query().Get("action") == "resize" { + //ResizeMachine + err := c.ResizeMachine(machineId, r.URL.Query().Get("package")) + if err != nil { + return err + } + return sendJSON(http.StatusAccepted, nil, w, r) + } else if r.URL.Query().Get("action") == "rename" { + //RenameMachine + err := c.RenameMachine(machineId, r.URL.Query().Get("name")) + if err != nil { + return err + } + return sendJSON(http.StatusAccepted, nil, w, r) + } else if r.URL.Query().Get("action") == "enable_firewall" { + //EnableFirewallMachine + err := c.EnableFirewallMachine(machineId) + if err != nil { + return err + } + return sendJSON(http.StatusAccepted, nil, w, r) + } else if r.URL.Query().Get("action") == "disable_firewall" { + //DisableFirewallMachine + err := c.DisableFirewallMachine(machineId) + if err != nil { + return err + } + return sendJSON(http.StatusAccepted, nil, w, r) + } else { + return ErrNotAllowed + } + case "PUT": + return ErrNotAllowed + case "DELETE": + if strings.HasSuffix(r.URL.Path, "machines") { + return ErrNotAllowed + } else { + // DeleteMachine + err := c.DeleteMachine(machineId) + if err != nil { + return err + } + return sendJSON(http.StatusNoContent, nil, w, r) + } + } + return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path) +} + +// handleFwRules handles the firewall rules HTTP API. +func (c *CloudAPI) handleFwRules(w http.ResponseWriter, r *http.Request) error { + prefix := fmt.Sprintf("/%s/fwrules/", c.ServiceInstance.UserAccount) + fwRuleId := strings.TrimPrefix(r.URL.Path, prefix) + switch r.Method { + case "GET": + if strings.HasSuffix(r.URL.Path, "fwrules") { + // ListFirewallRules + fwRules, err := c.ListFirewallRules() + if err != nil { + return err + } + if fwRules == nil { + fwRules = []*cloudapi.FirewallRule{} + } + resp := fwRules + return sendJSON(http.StatusOK, resp, w, r) + } else { + // GetFirewallRule + fwRule, err := c.GetFirewallRule(fwRuleId) + if err != nil { + return err + } + if fwRule == nil { + fwRule = &cloudapi.FirewallRule{} + } + resp := fwRule + return sendJSON(http.StatusOK, resp, w, r) + } + case "POST": + if strings.HasSuffix(r.URL.Path, "fwrules") { + // CreateFirewallRule + var ( + rule string + enabled bool + ) + opts := &cloudapi.CreateFwRuleOpts{} + body, errB := ioutil.ReadAll(r.Body) + if errB != nil { + return errB + } + if len(body) > 0 { + if errJ := json.Unmarshal(body, opts); errJ != nil { + return errJ + } + rule = opts.Rule + enabled = opts.Enabled + } + fwRule, err := c.CreateFirewallRule(rule, enabled) + if err != nil { + return err + } + if fwRule == nil { + fwRule = &cloudapi.FirewallRule{} + } + resp := fwRule + return sendJSON(http.StatusCreated, resp, w, r) + } else if strings.HasSuffix(r.URL.Path, "enable") { + // EnableFirewallRule + fwRuleId = strings.TrimSuffix(fwRuleId, "/enable") + fwRule, err := c.EnableFirewallRule(fwRuleId) + if err != nil { + return err + } + if fwRule == nil { + fwRule = &cloudapi.FirewallRule{} + } + resp := fwRule + return sendJSON(http.StatusOK, resp, w, r) + } else if strings.HasSuffix(r.URL.Path, "disable") { + // DisableFirewallRule + fwRuleId = strings.TrimSuffix(fwRuleId, "/disable") + fwRule, err := c.DisableFirewallRule(fwRuleId) + if err != nil { + return err + } + if fwRule == nil { + fwRule = &cloudapi.FirewallRule{} + } + resp := fwRule + return sendJSON(http.StatusOK, resp, w, r) + } else { + // UpdateFirewallRule + var ( + rule string + enabled bool + ) + opts := &cloudapi.CreateFwRuleOpts{} + body, errB := ioutil.ReadAll(r.Body) + if errB != nil { + return errB + } + if len(body) > 0 { + if errJ := json.Unmarshal(body, opts); errJ != nil { + return errJ + } + rule = opts.Rule + enabled = opts.Enabled + } + fwRule, err := c.UpdateFirewallRule(fwRuleId, rule, enabled) + if err != nil { + return err + } + if fwRule == nil { + fwRule = &cloudapi.FirewallRule{} + } + resp := fwRule + return sendJSON(http.StatusOK, resp, w, r) + } + case "PUT": + return ErrNotAllowed + case "DELETE": + if strings.HasSuffix(r.URL.Path, "fwrules") { + return ErrNotAllowed + } else { + // DeleteFirewallRule + err := c.DeleteFirewallRule(fwRuleId) + if err != nil { + return err + } + return sendJSON(http.StatusNoContent, nil, w, r) + } + } + return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path) +} + +// handleNetworks handles the networks HTTP API. +func (c *CloudAPI) handleNetworks(w http.ResponseWriter, r *http.Request) error { + prefix := fmt.Sprintf("/%s/networks/", c.ServiceInstance.UserAccount) + networkId := strings.TrimPrefix(r.URL.Path, prefix) + switch r.Method { + case "GET": + if strings.HasSuffix(r.URL.Path, "networks") { + // ListNetworks + networks, err := c.ListNetworks() + if err != nil { + return err + } + if networks == nil { + networks = []cloudapi.Network{} + } + resp := networks + return sendJSON(http.StatusOK, resp, w, r) + } else { + // GetNetwork + network, err := c.GetNetwork(networkId) + if err != nil { + return err + } + if network == nil { + network = &cloudapi.Network{} + } + resp := network + return sendJSON(http.StatusOK, resp, w, r) + } + case "POST": + return ErrNotAllowed + case "PUT": + return ErrNotAllowed + case "DELETE": + return ErrNotAllowed + } + return fmt.Errorf("unknown request method %q for %s", r.Method, r.URL.Path) +} + +// setupHTTP attaches all the needed handlers to provide the HTTP API. +func (c *CloudAPI) SetupHTTP(mux *http.ServeMux) { + handlers := map[string]http.Handler{ + "/": ErrNotFound, + "/$user/": ErrBadRequest, + "/$user/keys": c.handler((*CloudAPI).handleKeys), + "/$user/images": c.handler((*CloudAPI).handleImages), + "/$user/packages": c.handler((*CloudAPI).handlePackages), + "/$user/machines": c.handler((*CloudAPI).handleMachines), + //"/$user/datacenters": c.handler((*CloudAPI).handleDatacenters), + "/$user/fwrules": c.handler((*CloudAPI).handleFwRules), + "/$user/networks": c.handler((*CloudAPI).handleNetworks), + } + for path, h := range handlers { + path = strings.Replace(path, "$user", c.ServiceInstance.UserAccount, 1) + if !strings.HasSuffix(path, "/") { + mux.Handle(path+"/", h) + } + mux.Handle(path, h) + } +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/cloudapi/service_http_test.go juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/cloudapi/service_http_test.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/cloudapi/service_http_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/cloudapi/service_http_test.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,678 @@ +// +// gosdc - Go library to interact with the Joyent CloudAPI +// +// CloudAPI double testing service - HTTP API tests +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package cloudapi_test + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "path" + "strconv" + "strings" + + gc "launchpad.net/gocheck" + + "github.com/joyent/gocommon/testing" + "github.com/joyent/gosdc/cloudapi" + lc "github.com/joyent/gosdc/localservices/cloudapi" +) + +type CloudAPIHTTPSuite struct { + testing.HTTPSuite + service *lc.CloudAPI +} + +var _ = gc.Suite(&CloudAPIHTTPSuite{}) + +type CloudAPIHTTPSSuite struct { + testing.HTTPSuite + service *lc.CloudAPI +} + +var _ = gc.Suite(&CloudAPIHTTPSSuite{HTTPSuite: testing.HTTPSuite{UseTLS: true}}) + +func (s *CloudAPIHTTPSuite) SetUpSuite(c *gc.C) { + s.HTTPSuite.SetUpSuite(c) + c.Assert(s.Server.URL[:7], gc.Equals, "http://") + s.service = lc.New(s.Server.URL, testUserAccount) +} + +func (s *CloudAPIHTTPSuite) TearDownSuite(c *gc.C) { + s.HTTPSuite.TearDownSuite(c) +} + +func (s *CloudAPIHTTPSuite) SetUpTest(c *gc.C) { + s.HTTPSuite.SetUpTest(c) + s.service.SetupHTTP(s.Mux) +} + +func (s *CloudAPIHTTPSuite) TearDownTest(c *gc.C) { + s.HTTPSuite.TearDownTest(c) +} + +// assertJSON asserts the passed http.Response's body can be +// unmarshalled into the given expected object, populating it with the +// successfully parsed data. +func assertJSON(c *gc.C, resp *http.Response, expected interface{}) { + body, err := ioutil.ReadAll(resp.Body) + defer resp.Body.Close() + c.Assert(err, gc.IsNil) + err = json.Unmarshal(body, &expected) + c.Assert(err, gc.IsNil) +} + +// assertBody asserts the passed http.Response's body matches the +// expected response, replacing any variables in the expected body. +func assertBody(c *gc.C, resp *http.Response, expected *lc.ErrorResponse) { + body, err := ioutil.ReadAll(resp.Body) + defer resp.Body.Close() + c.Assert(err, gc.IsNil) + expBody := expected.Body + // cast to string for easier asserts debugging + c.Assert(string(body), gc.Equals, string(expBody)) +} + +// sendRequest constructs an HTTP request from the parameters and +// sends it, returning the response or an error. +func (s *CloudAPIHTTPSuite) sendRequest(method, path string, body []byte, headers http.Header) (*http.Response, error) { + if headers == nil { + headers = make(http.Header) + } + requestURL := "http://" + s.service.Hostname + strings.TrimLeft(path, "/") + req, err := http.NewRequest(method, requestURL, bytes.NewReader(body)) + if err != nil { + return nil, err + } + req.Close = true + for header, values := range headers { + for _, value := range values { + req.Header.Add(header, value) + } + } + // workaround for https://code.google.com/p/go/issues/detail?id=4454 + req.Header.Set("Content-Length", strconv.Itoa(len(body))) + return http.DefaultClient.Do(req) +} + +// jsonRequest serializes the passed body object to JSON and sends a +// the request with authRequest(). +func (s *CloudAPIHTTPSuite) jsonRequest(method, path string, body interface{}, headers http.Header) (*http.Response, error) { + jsonBody, err := json.Marshal(body) + if err != nil { + return nil, err + } + return s.sendRequest(method, path, jsonBody, headers) +} + +// Helpers +func (s *CloudAPIHTTPSuite) createKey(c *gc.C, keyName, key string) *cloudapi.Key { + opts := cloudapi.CreateKeyOpts{Name: keyName, Key: key} + optsByte, err := json.Marshal(opts) + c.Assert(err, gc.IsNil) + resp, err := s.sendRequest("POST", path.Join(testUserAccount, "keys"), optsByte, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusCreated) + k := &cloudapi.Key{} + assertJSON(c, resp, k) + + return k +} + +func (s *CloudAPIHTTPSuite) deleteKey(c *gc.C, keyName string) { + resp, err := s.sendRequest("DELETE", path.Join(testUserAccount, "keys", keyName), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusNoContent) +} + +func (s *CloudAPIHTTPSuite) createMachine(c *gc.C, name, pkg, image string, metadata, tags map[string]string) *cloudapi.Machine { + opts := cloudapi.CreateMachineOpts{Name: name, Image: image, Package: pkg, Tags: tags, Metadata: metadata} + optsByte, err := json.Marshal(opts) + c.Assert(err, gc.IsNil) + resp, err := s.sendRequest("POST", path.Join(testUserAccount, "machines"), optsByte, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusCreated) + m := &cloudapi.Machine{} + assertJSON(c, resp, m) + + return m +} + +func (s *CloudAPIHTTPSuite) getMachine(c *gc.C, machineId string, expected *cloudapi.Machine) { + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "machines", machineId), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, expected) +} + +func (s *CloudAPIHTTPSuite) deleteMachine(c *gc.C, machineId string) { + resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=stop", path.Join(testUserAccount, "machines", machineId)), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted) + resp, err = s.sendRequest("DELETE", path.Join(testUserAccount, "machines", machineId), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusNoContent) +} + +// Helper method to create a test firewall rule +func (s *CloudAPIHTTPSuite) createFirewallRule(c *gc.C) *cloudapi.FirewallRule { + opts := cloudapi.CreateFwRuleOpts{Rule: testFwRule, Enabled: true} + optsByte, err := json.Marshal(opts) + c.Assert(err, gc.IsNil) + resp, err := s.sendRequest("POST", path.Join(testUserAccount, "fwrules"), optsByte, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusCreated) + r := &cloudapi.FirewallRule{} + assertJSON(c, resp, r) + + return r +} + +// Helper method to a test firewall rule +func (s *CloudAPIHTTPSuite) deleteFwRule(c *gc.C, fwRuleId string) { + resp, err := s.sendRequest("DELETE", path.Join(testUserAccount, "fwrules", fwRuleId), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusNoContent) +} + +// SimpleTest defines a simple request without a body and expected response. +type SimpleTest struct { + method string + url string + headers http.Header + expect *lc.ErrorResponse +} + +func (s *CloudAPIHTTPSuite) simpleTests() []SimpleTest { + var simpleTests = []SimpleTest{ + { + method: "GET", + url: "/", + headers: make(http.Header), + expect: lc.ErrNotFound, + }, + { + method: "POST", + url: "/", + headers: make(http.Header), + expect: lc.ErrNotFound, + }, + { + method: "DELETE", + url: "/", + headers: make(http.Header), + expect: lc.ErrNotFound, + }, + { + method: "PUT", + url: "/", + headers: make(http.Header), + expect: lc.ErrNotFound, + }, + { + method: "GET", + url: "/any", + headers: make(http.Header), + expect: lc.ErrNotFound, + }, + { + method: "POST", + url: "/any", + headers: make(http.Header), + expect: lc.ErrNotFound, + }, + { + method: "DELETE", + url: "/any", + headers: make(http.Header), + expect: lc.ErrNotFound, + }, + { + method: "PUT", + url: "/any", + headers: make(http.Header), + expect: lc.ErrNotFound, + }, + { + method: "PUT", + url: path.Join(testUserAccount, "keys"), + headers: make(http.Header), + expect: lc.ErrNotAllowed, + }, + { + method: "PUT", + url: path.Join(testUserAccount, "images"), + headers: make(http.Header), + expect: lc.ErrNotAllowed, + }, + { + method: "POST", + url: path.Join(testUserAccount, "packages"), + headers: make(http.Header), + expect: lc.ErrNotAllowed, + }, + { + method: "PUT", + url: path.Join(testUserAccount, "packages"), + headers: make(http.Header), + expect: lc.ErrNotAllowed, + }, + { + method: "DELETE", + url: path.Join(testUserAccount, "packages"), + headers: make(http.Header), + expect: lc.ErrNotAllowed, + }, + { + method: "PUT", + url: path.Join(testUserAccount, "machines"), + headers: make(http.Header), + expect: lc.ErrNotAllowed, + }, + { + method: "PUT", + url: path.Join(testUserAccount, "fwrules"), + headers: make(http.Header), + expect: lc.ErrNotAllowed, + }, + { + method: "POST", + url: path.Join(testUserAccount, "networks"), + headers: make(http.Header), + expect: lc.ErrNotAllowed, + }, + { + method: "PUT", + url: path.Join(testUserAccount, "networks"), + headers: make(http.Header), + expect: lc.ErrNotAllowed, + }, + { + method: "DELETE", + url: path.Join(testUserAccount, "networks"), + headers: make(http.Header), + expect: lc.ErrNotAllowed, + }, + } + return simpleTests +} + +func (s *CloudAPIHTTPSuite) TestSimpleRequestTests(c *gc.C) { + simpleTests := s.simpleTests() + for i, t := range simpleTests { + c.Logf("#%d. %s %s -> %d", i, t.method, t.url, t.expect.Code) + if t.headers == nil { + t.headers = make(http.Header) + } + var ( + resp *http.Response + err error + ) + resp, err = s.sendRequest(t.method, t.url, nil, t.headers) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, t.expect.Code) + assertBody(c, resp, t.expect) + } +} + +// Tests for Keys API +func (s *CloudAPIHTTPSuite) TestListKeys(c *gc.C) { + var expected []cloudapi.Key + k := s.createKey(c, testKeyName, testKey) + defer s.deleteKey(c, testKeyName) + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "keys"), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + for _, key := range expected { + if c.Check(&key, gc.DeepEquals, k) { + c.SucceedNow() + } + } + c.Fatalf("Obtained keys [%s] do not contain test key [%s]", expected, k) +} + +func (s *CloudAPIHTTPSuite) TestGetKey(c *gc.C) { + var expected cloudapi.Key + k := s.createKey(c, testKeyName, testKey) + defer s.deleteKey(c, testKeyName) + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "keys", testKeyName), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(&expected, gc.DeepEquals, k) +} + +func (s *CloudAPIHTTPSuite) TestCreateKey(c *gc.C) { + k := s.createKey(c, testKeyName, testKey) + defer s.deleteKey(c, testKeyName) + + c.Assert(k.Name, gc.Equals, testKeyName) + c.Assert(k.Key, gc.Equals, testKey) +} + +func (s *CloudAPIHTTPSuite) TestDeleteKey(c *gc.C) { + s.createKey(c, testKeyName, testKey) + s.deleteKey(c, testKeyName) +} + +// Tests for Images API +func (s *CloudAPIHTTPSuite) TestListImages(c *gc.C) { + var expected []cloudapi.Image + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "images"), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(len(expected), gc.Equals, 6) +} + +func (s *CloudAPIHTTPSuite) TestGetImage(c *gc.C) { + var expected cloudapi.Image + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "images", testImage), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(expected.Id, gc.Equals, "11223344-0a0a-ff99-11bb-0a1b2c3d4e5f") + c.Assert(expected.Name, gc.Equals, "ubuntu12.04") + c.Assert(expected.OS, gc.Equals, "linux") + c.Assert(expected.Version, gc.Equals, "2.3.1") + c.Assert(expected.Type, gc.Equals, "virtualmachine") + c.Assert(expected.Description, gc.Equals, "Test Ubuntu 12.04 image (64 bit)") + c.Assert(expected.PublishedAt, gc.Equals, "2014-01-20T16:12:31Z") + c.Assert(expected.Public, gc.Equals, "true") + c.Assert(expected.State, gc.Equals, "active") +} + +// Tests for Packages API +func (s *CloudAPIHTTPSuite) TestListPackages(c *gc.C) { + var expected []cloudapi.Package + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "packages"), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(len(expected), gc.Equals, 4) +} + +func (s *CloudAPIHTTPSuite) TestGetPackage(c *gc.C) { + var expected cloudapi.Package + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "packages", testPackage), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(expected.Name, gc.Equals, "Small") + c.Assert(expected.Memory, gc.Equals, 1024) + c.Assert(expected.Disk, gc.Equals, 16384) + c.Assert(expected.Swap, gc.Equals, 2048) + c.Assert(expected.VCPUs, gc.Equals, 1) + c.Assert(expected.Default, gc.Equals, true) + c.Assert(expected.Id, gc.Equals, "11223344-1212-abab-3434-aabbccddeeff") + c.Assert(expected.Version, gc.Equals, "1.0.2") +} + +// Tests for Machines API +func (s *CloudAPIHTTPSuite) TestListMachines(c *gc.C) { + var expected []cloudapi.Machine + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "machines"), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + for _, machine := range expected { + if machine.Id == m.Id { + c.SucceedNow() + } + } + c.Fatalf("Obtained machine [%s] do not contain test machine [%s]", expected, m) +} + +/*func (s *CloudAPIHTTPSuite) TestCountMachines(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + resp, err := s.sendRequest("HEAD", path.Join(testUserAccount, "machines"), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + fmt.Printf("Got response %q\n", resp) + assertBody(c, resp, &lc.ErrorResponse{Body: "1"}) +} */ + +func (s *CloudAPIHTTPSuite) TestGetMachine(c *gc.C) { + var expected cloudapi.Machine + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + s.getMachine(c, m.Id, &expected) + c.Assert(expected.Name, gc.Equals, testMachineName) + c.Assert(expected.Package, gc.Equals, testPackage) + c.Assert(expected.Image, gc.Equals, testImage) +} + +func (s *CloudAPIHTTPSuite) TestCreateMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + c.Assert(m.Name, gc.Equals, testMachineName) + c.Assert(m.Package, gc.Equals, testPackage) + c.Assert(m.Image, gc.Equals, testImage) + c.Assert(m.Type, gc.Equals, "virtualmachine") + c.Assert(len(m.IPs), gc.Equals, 2) + c.Assert(m.State, gc.Equals, "running") +} + +func (s *CloudAPIHTTPSuite) TestStartMachine(c *gc.C) { + var expected cloudapi.Machine + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=start", path.Join(testUserAccount, "machines", m.Id)), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted) + + s.getMachine(c, m.Id, &expected) + c.Assert(expected.State, gc.Equals, "running") +} + +func (s *CloudAPIHTTPSuite) TestStopMachine(c *gc.C) { + var expected cloudapi.Machine + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=stop", path.Join(testUserAccount, "machines", m.Id)), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted) + + s.getMachine(c, m.Id, &expected) + c.Assert(expected.State, gc.Equals, "stopped") +} + +func (s *CloudAPIHTTPSuite) TestRebootMachine(c *gc.C) { + var expected cloudapi.Machine + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=reboot", path.Join(testUserAccount, "machines", m.Id)), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted) + + s.getMachine(c, m.Id, &expected) + c.Assert(expected.State, gc.Equals, "running") +} + +func (s *CloudAPIHTTPSuite) TestResizeMachine(c *gc.C) { + var expected cloudapi.Machine + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=resize&package=Medium", path.Join(testUserAccount, "machines", m.Id)), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted) + + s.getMachine(c, m.Id, &expected) + c.Assert(expected.Package, gc.Equals, "Medium") + c.Assert(expected.Memory, gc.Equals, 2048) + c.Assert(expected.Disk, gc.Equals, 32768) +} + +func (s *CloudAPIHTTPSuite) TestRenameMachine(c *gc.C) { + var expected cloudapi.Machine + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=rename&name=new-test-name", path.Join(testUserAccount, "machines", m.Id)), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted) + + s.getMachine(c, m.Id, &expected) + c.Assert(expected.Name, gc.Equals, "new-test-name") +} + +func (s *CloudAPIHTTPSuite) TestListMachinesFirewallRules(c *gc.C) { + var expected []cloudapi.FirewallRule + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "machines", m.Id, "fwrules"), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) +} + +func (s *CloudAPIHTTPSuite) TestEnableFirewallMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=enable_firewall", path.Join(testUserAccount, "machines", m.Id)), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted) +} + +func (s *CloudAPIHTTPSuite) TestDisableFirewallMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + resp, err := s.sendRequest("POST", fmt.Sprintf("%s?action=disable_firewall", path.Join(testUserAccount, "machines", m.Id)), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusAccepted) +} + +func (s *CloudAPIHTTPSuite) TestDeleteMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + s.deleteMachine(c, m.Id) +} + +// Tests for FirewallRules API +func (s *CloudAPIHTTPSuite) TestCreateFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + // cleanup + s.deleteFwRule(c, testFwRule.Id) +} + +func (s *CloudAPIHTTPSuite) TestListFirewallRules(c *gc.C) { + var expected []cloudapi.FirewallRule + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "fwrules"), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) +} + +func (s *CloudAPIHTTPSuite) TestGetFirewallRule(c *gc.C) { + var expected cloudapi.FirewallRule + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "fwrules", testFwRule.Id), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) +} + +func (s *CloudAPIHTTPSuite) TestUpdateFirewallRule(c *gc.C) { + var expected cloudapi.FirewallRule + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + opts := cloudapi.CreateFwRuleOpts{Rule: testUpdatedFwRule, Enabled: true} + optsByte, err := json.Marshal(opts) + c.Assert(err, gc.IsNil) + resp, err := s.sendRequest("POST", path.Join(testUserAccount, "fwrules", testFwRule.Id), optsByte, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(expected.Rule, gc.Equals, testUpdatedFwRule) +} + +func (s *CloudAPIHTTPSuite) TestEnableFirewallRule(c *gc.C) { + var expected cloudapi.FirewallRule + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + resp, err := s.sendRequest("POST", path.Join(testUserAccount, "fwrules", testFwRule.Id, "enable"), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(expected.Enabled, gc.Equals, true) +} + +func (s *CloudAPIHTTPSuite) TestDisableFirewallRule(c *gc.C) { + var expected cloudapi.FirewallRule + testFwRule := s.createFirewallRule(c) + defer s.deleteFwRule(c, testFwRule.Id) + + resp, err := s.sendRequest("POST", path.Join(testUserAccount, "fwrules", testFwRule.Id, "disable"), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(expected.Enabled, gc.Equals, false) +} + +func (s *CloudAPIHTTPSuite) TestDeleteFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + s.deleteFwRule(c, testFwRule.Id) +} + +// tests for Networks API +func (s *CloudAPIHTTPSuite) TestListNetworks(c *gc.C) { + var expected []cloudapi.Network + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "networks"), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) +} + +func (s *CloudAPIHTTPSuite) TestGetNetwork(c *gc.C) { + var expected cloudapi.Network + + resp, err := s.sendRequest("GET", path.Join(testUserAccount, "networks", testNetworkId), nil, nil) + c.Assert(err, gc.IsNil) + c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) + assertJSON(c, resp, &expected) + c.Assert(expected, gc.DeepEquals, cloudapi.Network{ + Id: testNetworkId, + Name: "Test-Joyent-Public", + Public: true, + Description: "", + }) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/cloudapi/service_test.go juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/cloudapi/service_test.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/cloudapi/service_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/cloudapi/service_test.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,420 @@ +// +// gosdc - Go library to interact with the Joyent CloudAPI +// +// CloudAPI double testing service - internal direct API test +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package cloudapi_test + +import ( + gc "launchpad.net/gocheck" + "testing" + + "github.com/joyent/gosdc/cloudapi" + lc "github.com/joyent/gosdc/localservices/cloudapi" +) + +type CloudAPISuite struct { + service *lc.CloudAPI +} + +const ( + testServiceURL = "https://go-test.api.joyentcloud.com" + testUserAccount = "gouser" + testKeyName = "test-key" + testKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLF4s7GLYfPYVr3zqZcNCZcM2qFDXXxE5pCGuGowySGKTnxrqrPY4HO+9CQ+5X55o4rJOfNJ9ZRa+2Qmlr4F/qACcT/ZJbXPs+LcbOVtgUaynn6ooh0C4V/MdKPZmW8FSTy98GstVJZXJO2gJwlKGHQwoWuZ5H/IeN6gCaXi65NPg4eu3Ls9a6BtvOf1Vtb1wwl7QqoZqziT5omA9bDeoBXdoYOoowDS3LUprFRvc1lW7fY9eLKNvoQ4oOJMMn5cPh2CICj5cb2eRH1pHJA/9mxxW4+bB7QdL3N7hDbpV4Qz5MxjxYN3DWldvyP1zCe/Tgyduiz4X3gDBhy735Bpat gouser@localhost" + testPackage = "Small" + testImage = "11223344-0a0a-ff99-11bb-0a1b2c3d4e5f" + testMachineName = "test-machine" + testFwRule = "FROM subnet 10.35.76.0/24 TO subnet 10.35.101.0/24 ALLOW tcp (PORT 80 AND PORT 443)" + testUpdatedFwRule = "FROM subnet 10.35.76.0/24 TO subnet 10.35.101.0/24 ALLOW tcp (port 80 AND port 443 AND port 8080)" + testNetworkId = "123abc4d-0011-aabb-2233-ccdd4455" +) + +var _ = gc.Suite(&CloudAPISuite{}) + +func Test(t *testing.T) { + gc.TestingT(t) +} + +func (s *CloudAPISuite) SetUpSuite(c *gc.C) { + s.service = lc.New(testServiceURL, testUserAccount) +} + +// Helpers +func (s *CloudAPISuite) createKey(c *gc.C, keyName, key string) *cloudapi.Key { + k, err := s.service.CreateKey(keyName, key) + c.Assert(err, gc.IsNil) + + return k +} + +func (s *CloudAPISuite) deleteKey(c *gc.C, keyName string) { + err := s.service.DeleteKey(keyName) + c.Assert(err, gc.IsNil) +} + +func (s *CloudAPISuite) createMachine(c *gc.C, name, pkg, image string, metadata, tags map[string]string) *cloudapi.Machine { + m, err := s.service.CreateMachine(name, pkg, image, metadata, tags) + c.Assert(err, gc.IsNil) + + return m +} + +func (s *CloudAPISuite) deleteMachine(c *gc.C, machineId string) { + err := s.service.StopMachine(machineId) + c.Assert(err, gc.IsNil) + err = s.service.DeleteMachine(machineId) + c.Assert(err, gc.IsNil) +} + +// Helper method to create a test firewall rule +func (s *CloudAPISuite) createFirewallRule(c *gc.C) *cloudapi.FirewallRule { + fwRule, err := s.service.CreateFirewallRule(testFwRule, false) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) + c.Assert(fwRule.Rule, gc.Equals, testFwRule) + c.Assert(fwRule.Enabled, gc.Equals, false) + + return fwRule +} + +// Helper method to a test firewall rule +func (s *CloudAPISuite) deleteFwRule(c *gc.C, fwRuleId string) { + err := s.service.DeleteFirewallRule(fwRuleId) + c.Assert(err, gc.IsNil) +} + +// Tests for Keys API +func (s *CloudAPISuite) TestListKeys(c *gc.C) { + k := s.createKey(c, testKeyName, testKey) + defer s.deleteKey(c, testKeyName) + + keys, err := s.service.ListKeys() + c.Assert(err, gc.IsNil) + for _, key := range keys { + if c.Check(&key, gc.DeepEquals, k) { + c.SucceedNow() + } + } + c.Fatalf("Obtained keys [%s] do not contain test key [%s]", keys, k) +} + +func (s *CloudAPISuite) TestGetKey(c *gc.C) { + k := s.createKey(c, testKeyName, testKey) + defer s.deleteKey(c, testKeyName) + + key, err := s.service.GetKey(testKeyName) + c.Assert(err, gc.IsNil) + c.Assert(key, gc.DeepEquals, k) +} + +func (s *CloudAPISuite) TestCreateKey(c *gc.C) { + k := s.createKey(c, testKeyName, testKey) + defer s.deleteKey(c, testKeyName) + + c.Assert(k.Name, gc.Equals, testKeyName) + c.Assert(k.Key, gc.Equals, testKey) +} + +func (s *CloudAPISuite) TestDeleteKey(c *gc.C) { + s.createKey(c, testKeyName, testKey) + s.deleteKey(c, testKeyName) +} + +// Tests for Package API +func (s *CloudAPISuite) TestListPackages(c *gc.C) { + pkgs, err := s.service.ListPackages(nil) + c.Assert(err, gc.IsNil) + c.Assert(len(pkgs), gc.Equals, 4) +} + +func (s *CloudAPISuite) TestListPackagesWithFilter(c *gc.C) { + pkgs, err := s.service.ListPackages(map[string]string{"memory": "1024"}) + c.Assert(err, gc.IsNil) + c.Assert(len(pkgs), gc.Equals, 1) +} + +func (s *CloudAPISuite) TestGetPackage(c *gc.C) { + pkg, err := s.service.GetPackage(testPackage) + c.Assert(err, gc.IsNil) + c.Assert(pkg.Name, gc.Equals, "Small") + c.Assert(pkg.Memory, gc.Equals, 1024) + c.Assert(pkg.Disk, gc.Equals, 16384) + c.Assert(pkg.Swap, gc.Equals, 2048) + c.Assert(pkg.VCPUs, gc.Equals, 1) + c.Assert(pkg.Default, gc.Equals, true) + c.Assert(pkg.Id, gc.Equals, "11223344-1212-abab-3434-aabbccddeeff") + c.Assert(pkg.Version, gc.Equals, "1.0.2") +} + +// Tests for Images API +func (s *CloudAPISuite) TestListImages(c *gc.C) { + images, err := s.service.ListImages(nil) + c.Assert(err, gc.IsNil) + c.Assert(len(images), gc.Equals, 6) +} + +func (s *CloudAPISuite) TestListImagesWithFilter(c *gc.C) { + images, err := s.service.ListImages(map[string]string{"os": "linux"}) + c.Assert(err, gc.IsNil) + c.Assert(len(images), gc.Equals, 4) +} + +func (s *CloudAPISuite) TestGetImage(c *gc.C) { + image, err := s.service.GetImage(testImage) + c.Assert(err, gc.IsNil) + c.Assert(image.Id, gc.Equals, "11223344-0a0a-ff99-11bb-0a1b2c3d4e5f") + c.Assert(image.Name, gc.Equals, "ubuntu12.04") + c.Assert(image.OS, gc.Equals, "linux") + c.Assert(image.Version, gc.Equals, "2.3.1") + c.Assert(image.Type, gc.Equals, "virtualmachine") + c.Assert(image.Description, gc.Equals, "Test Ubuntu 12.04 image (64 bit)") + c.Assert(image.PublishedAt, gc.Equals, "2014-01-20T16:12:31Z") + c.Assert(image.Public, gc.Equals, "true") + c.Assert(image.State, gc.Equals, "active") +} + +// Test for Machine API +func (s *CloudAPISuite) TestListMachines(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + machines, err := s.service.ListMachines(nil) + c.Assert(err, gc.IsNil) + for _, machine := range machines { + if machine.Id == m.Id { + c.SucceedNow() + } + } + c.Fatalf("Obtained machine [%s] do not contain test machine [%s]", machines, m) +} + +func (s *CloudAPISuite) TestCountMachines(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + count, err := s.service.CountMachines() + c.Assert(err, gc.IsNil) + c.Assert(count, gc.Equals, 1) +} + +func (s *CloudAPISuite) TestGetMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + machine, err := s.service.GetMachine(m.Id) + c.Assert(err, gc.IsNil) + c.Assert(machine.Name, gc.Equals, testMachineName) + c.Assert(machine.Package, gc.Equals, testPackage) + c.Assert(machine.Image, gc.Equals, testImage) +} + +func (s *CloudAPISuite) TestCreateMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + c.Assert(m.Name, gc.Equals, testMachineName) + c.Assert(m.Package, gc.Equals, testPackage) + c.Assert(m.Image, gc.Equals, testImage) + c.Assert(m.Type, gc.Equals, "virtualmachine") + c.Assert(len(m.IPs), gc.Equals, 2) + c.Assert(m.State, gc.Equals, "running") +} + +func (s *CloudAPISuite) TestStartMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + err := s.service.StartMachine(m.Id) + c.Assert(err, gc.IsNil) + + machine, err := s.service.GetMachine(m.Id) + c.Assert(err, gc.IsNil) + c.Assert(machine.State, gc.Equals, "running") +} + +func (s *CloudAPISuite) TestStopMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + err := s.service.StopMachine(m.Id) + c.Assert(err, gc.IsNil) + + machine, err := s.service.GetMachine(m.Id) + c.Assert(err, gc.IsNil) + c.Assert(machine.State, gc.Equals, "stopped") +} + +func (s *CloudAPISuite) TestRebootMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + err := s.service.RebootMachine(m.Id) + c.Assert(err, gc.IsNil) + + machine, err := s.service.GetMachine(m.Id) + c.Assert(err, gc.IsNil) + c.Assert(machine.State, gc.Equals, "running") +} + +func (s *CloudAPISuite) TestResizeMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + err := s.service.ResizeMachine(m.Id, "Medium") + c.Assert(err, gc.IsNil) + + machine, err := s.service.GetMachine(m.Id) + c.Assert(err, gc.IsNil) + c.Assert(machine.Package, gc.Equals, "Medium") + c.Assert(machine.Memory, gc.Equals, 2048) + c.Assert(machine.Disk, gc.Equals, 32768) +} + +func (s *CloudAPISuite) TestRenameMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + err := s.service.RenameMachine(m.Id, "new-test-name") + c.Assert(err, gc.IsNil) + + machine, err := s.service.GetMachine(m.Id) + c.Assert(err, gc.IsNil) + c.Assert(machine.Name, gc.Equals, "new-test-name") +} + +func (s *CloudAPISuite) TestListMachinesFirewallRules(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + fwRules, err := s.service.ListMachineFirewallRules(m.Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRules, gc.NotNil) +} + +func (s *CloudAPISuite) TestEnableFirewallMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + err := s.service.EnableFirewallMachine(m.Id) + c.Assert(err, gc.IsNil) +} + +func (s *CloudAPISuite) TestDisableFirewallMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + defer s.deleteMachine(c, m.Id) + + err := s.service.DisableFirewallMachine(m.Id) + c.Assert(err, gc.IsNil) +} + +func (s *CloudAPISuite) TestDeleteMachine(c *gc.C) { + m := s.createMachine(c, testMachineName, testPackage, testImage, nil, nil) + s.deleteMachine(c, m.Id) +} + +// Tests for FirewallRules API +func (s *CloudAPISuite) TestCreateFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + // cleanup + s.deleteFwRule(c, testFwRule.Id) +} + +func (s *CloudAPISuite) TestListFirewallRules(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + rules, err := s.service.ListFirewallRules() + c.Assert(err, gc.IsNil) + c.Assert(rules, gc.NotNil) + + // cleanup + s.deleteFwRule(c, testFwRule.Id) +} + +func (s *CloudAPISuite) TestGetFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + fwRule, err := s.service.GetFirewallRule(testFwRule.Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) + c.Assert((*fwRule), gc.DeepEquals, (*testFwRule)) + + // cleanup + s.deleteFwRule(c, testFwRule.Id) +} + +func (s *CloudAPISuite) TestUpdateFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + fwRule, err := s.service.UpdateFirewallRule(testFwRule.Id, testUpdatedFwRule, true) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) + c.Assert(fwRule.Rule, gc.Equals, testUpdatedFwRule) + + // cleanup + s.deleteFwRule(c, testFwRule.Id) +} + +func (s *CloudAPISuite) TestEnableFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + fwRule, err := s.service.EnableFirewallRule((*testFwRule).Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) + + // cleanup + s.deleteFwRule(c, testFwRule.Id) +} + +func (s *CloudAPISuite) TestListFirewallRuleMachines(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + machines, err := s.service.ListFirewallRuleMachines((*testFwRule).Id) + c.Assert(err, gc.IsNil) + c.Assert(machines, gc.NotNil) + + // cleanup + s.deleteFwRule(c, testFwRule.Id) +} + +func (s *CloudAPISuite) TestDisableFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + fwRule, err := s.service.DisableFirewallRule((*testFwRule).Id) + c.Assert(err, gc.IsNil) + c.Assert(fwRule, gc.NotNil) + + // cleanup + s.deleteFwRule(c, testFwRule.Id) +} + +func (s *CloudAPISuite) TestDeleteFirewallRule(c *gc.C) { + testFwRule := s.createFirewallRule(c) + + s.deleteFwRule(c, testFwRule.Id) +} + +// tests for Networks API +func (s *CloudAPISuite) TestListNetworks(c *gc.C) { + nets, err := s.service.ListNetworks() + c.Assert(err, gc.IsNil) + c.Assert(nets, gc.NotNil) +} + +func (s *CloudAPISuite) TestGetNetwork(c *gc.C) { + net, err := s.service.GetNetwork(testNetworkId) + c.Assert(err, gc.IsNil) + c.Assert(net, gc.NotNil) + c.Assert(net, gc.DeepEquals, &cloudapi.Network{ + Id: testNetworkId, + Name: "Test-Joyent-Public", + Public: true, + Description: "", + }) +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/hook/service.go juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/hook/service.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/hook/service.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/hook/service.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,86 @@ +package hook + +import ( + "runtime" + "strings" +) + +type TestService struct { + ServiceControl + // Hooks to run when specified control points are reached in the service business logic. + ControlHooks map[string]ControlProcessor +} + +// ControlProcessor defines a function that is run when a specified control point is reached in the service +// business logic. The function receives the service instance so internal state can be inspected, plus for any +// arguments passed to the currently executing service function. +type ControlProcessor func(sc ServiceControl, args ...interface{}) error + +// ControlHookCleanup defines a function used to remove a control hook. +type ControlHookCleanup func() + +// ServiceControl instances allow hooks to be registered for execution at the specified point of execution. +// The control point name can be a function name or a logical execution point meaningful to the service. +// If name is "", the hook for the currently executing function is executed. +// Returns a function which can be used to remove the hook. +type ServiceControl interface { + RegisterControlPoint(name string, controller ControlProcessor) ControlHookCleanup +} + +// currentServiceMethodName returns the method executing on the service when ProcessControlHook was invoked. +func (s *TestService) currentServiceMethodName() string { + pc, _, _, ok := runtime.Caller(2) + if !ok { + panic("current method name cannot be found") + } + return unqualifiedMethodName(pc) +} + +func unqualifiedMethodName(pc uintptr) string { + f := runtime.FuncForPC(pc) + fullName := f.Name() + nameParts := strings.Split(fullName, ".") + return nameParts[len(nameParts)-1] +} + +// ProcessControlHook retrieves the ControlProcessor for the specified hook name and runs it, returning any error. +// Use it like this to invoke a hook registered for some arbitrary control point: +// if err := n.ProcessControlHook("foobar", , , ); err != nil { +// return err +// } +func (s *TestService) ProcessControlHook(hookName string, sc ServiceControl, args ...interface{}) error { + if s.ControlHooks == nil { + return nil + } + if hook, ok := s.ControlHooks[hookName]; ok { + return hook(sc, args...) + } + return nil +} + +// ProcessFunctionHook runs the ControlProcessor for the current function, returning any error. +// Use it like this: +// if err := n.ProcessFunctionHook(, , ); err != nil { +// return err +// } +func (s *TestService) ProcessFunctionHook(sc ServiceControl, args ...interface{}) error { + hookName := s.currentServiceMethodName() + return s.ProcessControlHook(hookName, sc, args...) +} + +// RegisterControlPoint assigns the specified controller to the named hook. If nil, any existing controller for the +// hook is removed. +// hookName is the name of a function on the service or some arbitrarily named control point. +func (s *TestService) RegisterControlPoint(hookName string, controller ControlProcessor) ControlHookCleanup { + if s.ControlHooks == nil { + s.ControlHooks = make(map[string]ControlProcessor) + } + if controller == nil { + delete(s.ControlHooks, hookName) + } else { + s.ControlHooks[hookName] = controller + } + return func() { + s.RegisterControlPoint(hookName, nil) + } +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/hook/service_test.go juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/hook/service_test.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/hook/service_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/hook/service_test.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,105 @@ +package hook + +import ( + "fmt" + gc "launchpad.net/gocheck" + "testing" +) + +func Test(t *testing.T) { + gc.TestingT(t) +} + +var _ = gc.Suite(&ServiceSuite{}) + +type ServiceSuite struct { + ts *testService +} + +func (s *ServiceSuite) SetUpTest(c *gc.C) { + s.ts = newTestService() + // This hook is called based on the function name. + s.ts.RegisterControlPoint("foo", functionControlHook) + // This hook is called based on a user specified hook name. + s.ts.RegisterControlPoint("foobar", namedControlHook) +} + +type testService struct { + TestService + label string +} + +func newTestService() *testService { + return &testService{ + TestService: TestService{ + ControlHooks: make(map[string]ControlProcessor), + }, + } +} + +func functionControlHook(s ServiceControl, args ...interface{}) error { + label := args[0].(string) + returnError := args[1].(bool) + if returnError { + return fmt.Errorf("An error occurred") + } + s.(*testService).label = label + return nil +} + +func namedControlHook(s ServiceControl, args ...interface{}) error { + s.(*testService).label = "foobar" + return nil +} + +func (s *testService) foo(label string, returnError bool) error { + if err := s.ProcessFunctionHook(s, label, returnError); err != nil { + return err + } + return nil +} + +func (s *testService) bar() error { + if err := s.ProcessControlHook("foobar", s); err != nil { + return err + } + return nil +} + +func (s *ServiceSuite) TestFunctionHookNoError(c *gc.C) { + err := s.ts.foo("success", false) + c.Assert(err, gc.IsNil) + c.Assert(s.ts.label, gc.Equals, "success") +} + +func (s *ServiceSuite) TestHookWithError(c *gc.C) { + err := s.ts.foo("success", true) + c.Assert(err, gc.Not(gc.IsNil)) + c.Assert(s.ts.label, gc.Equals, "") +} + +func (s *ServiceSuite) TestNamedHook(c *gc.C) { + err := s.ts.bar() + c.Assert(err, gc.IsNil) + c.Assert(s.ts.label, gc.Equals, "foobar") +} + +func (s *ServiceSuite) TestHookCleanup(c *gc.C) { + // Manually delete the existing control point. + s.ts.RegisterControlPoint("foo", nil) + // Register a new hook and ensure it works. + cleanup := s.ts.RegisterControlPoint("foo", functionControlHook) + err := s.ts.foo("cleanuptest", false) + c.Assert(err, gc.IsNil) + c.Assert(s.ts.label, gc.Equals, "cleanuptest") + // Use the cleanup func to remove the hook and check the result. + cleanup() + err = s.ts.foo("again", false) + c.Assert(err, gc.IsNil) + c.Assert(s.ts.label, gc.Equals, "cleanuptest") + // Ensure that only the specified hook was removed and the other remaining one still works. + err = s.ts.bar() + c.Assert(err, gc.IsNil) + c.Assert(s.ts.label, gc.Equals, "foobar") + +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/localservice.go juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/localservice.go --- juju-core-1.17.7/src/github.com/joyent/gosdc/localservices/localservice.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/localservices/localservice.go 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,45 @@ +// +// gosdc - Go library to interact with the Joyent CloudAPI +// +// Double testing service +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package localservices + +import ( + "crypto/rand" + "fmt" + "io" + "net/http" + + "github.com/joyent/gosdc/localservices/hook" +) + +// An HttpService provides the HTTP API for a service double. +type HttpService interface { + SetupHTTP(mux *http.ServeMux) +} + +// A ServiceInstance is an Joyent Cloud service. +type ServiceInstance struct { + hook.TestService + Scheme string + Hostname string + UserAccount string +} + +// NewUUID generates a random UUID according to RFC 4122 +func NewUUID() (string, error) { + uuid := make([]byte, 16) + n, err := io.ReadFull(rand.Reader, uuid) + if n != len(uuid) || err != nil { + return "", err + } + uuid[8] = uuid[8]&^0xc0 | 0x80 + uuid[6] = uuid[6]&^0xf0 | 0x40 + return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosdc/README.md juju-core-1.18.0/src/github.com/joyent/gosdc/README.md --- juju-core-1.17.7/src/github.com/joyent/gosdc/README.md 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosdc/README.md 2014-04-04 16:56:42.000000000 +0000 @@ -0,0 +1,2 @@ +gosdc +===== diff -Nru juju-core-1.17.7/src/github.com/joyent/gosign/auth/auth.go juju-core-1.18.0/src/github.com/joyent/gosign/auth/auth.go --- juju-core-1.17.7/src/github.com/joyent/gosign/auth/auth.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosign/auth/auth.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,132 @@ +// +// gosign - Go HTTP signing library for the Joyent Public Cloud and Joyent Manta +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package auth + +import ( + "crypto" + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/base64" + "encoding/pem" + "fmt" + "net/http" + "net/url" + "strings" +) + +const ( + // Authorization Headers + SdcSignature = "Signature keyId=\"/%s/keys/%s\",algorithm=\"%s\" %s" + MantaSignature = "Signature keyId=\"/%s/keys/%s\",algorithm=\"%s\",signature=\"%s\"" +) + +type Endpoint struct { + URL string +} + +type Auth struct { + User string + PrivateKey string + Algorithm string +} + +type Credentials struct { + UserAuthentication Auth + SdcKeyId string + SdcEndpoint Endpoint + MantaKeyId string + MantaEndpoint Endpoint +} + +type PrivateKey struct { + key *rsa.PrivateKey +} + +// The CreateAuthorizationHeader returns the Authorization header for the give request. +func CreateAuthorizationHeader(headers http.Header, credentials *Credentials, isMantaRequest bool) (string, error) { + if isMantaRequest { + signature, err := GetSignature(&credentials.UserAuthentication, "date: "+headers.Get("Date")) + if err != nil { + return "", err + } + return fmt.Sprintf(MantaSignature, credentials.UserAuthentication.User, credentials.MantaKeyId, + credentials.UserAuthentication.Algorithm, signature), nil + } + signature, err := GetSignature(&credentials.UserAuthentication, headers.Get("Date")) + if err != nil { + return "", err + } + return fmt.Sprintf(SdcSignature, credentials.UserAuthentication.User, credentials.SdcKeyId, + credentials.UserAuthentication.Algorithm, signature), nil +} + +// The GetSignature method signs the specified key according to http://apidocs.joyent.com/cloudapi/#issuing-requests +// and http://apidocs.joyent.com/manta/api.html#authentication. +func GetSignature(auth *Auth, signing string) (string, error) { + block, _ := pem.Decode([]byte(auth.PrivateKey)) + if block == nil { + return "", fmt.Errorf("invalid private key data: %s", auth.PrivateKey) + } + rsakey, err := x509.ParsePKCS1PrivateKey(block.Bytes) + if err != nil { + return "", fmt.Errorf("An error occurred while parsing the key: %s", err) + } + privateKey := &PrivateKey{rsakey} + + hashFunc := getHashFunction(auth.Algorithm) + hash := hashFunc.New() + hash.Write([]byte(signing)) + + digest := hash.Sum(nil) + + signed, err := rsa.SignPKCS1v15(rand.Reader, privateKey.key, hashFunc, digest) + if err != nil { + return "", fmt.Errorf("An error occurred while signing the key: %s", err) + } + + return base64.StdEncoding.EncodeToString(signed), nil +} + +// Helper method to get the Hash function based on the algorithm +func getHashFunction(algorithm string) (hashFunc crypto.Hash) { + switch strings.ToLower(algorithm) { + case "rsa-sha1": + hashFunc = crypto.SHA1 + case "rsa-sha224", "rsa-sha256": + hashFunc = crypto.SHA256 + case "rsa-sha384", "rsa-sha512": + hashFunc = crypto.SHA512 + default: + hashFunc = crypto.SHA256 + } + return +} + +func (cred *Credentials) Region() string { + sdcUrl := cred.SdcEndpoint.URL + + if isLocalhost(sdcUrl) { + return "some-region" + } + return sdcUrl[strings.LastIndex(sdcUrl, "/")+1 : strings.Index(sdcUrl, ".")] +} + +func isLocalhost(u string) bool { + parsedUrl, err := url.Parse(u) + if err != nil { + return false + } + if strings.HasPrefix(parsedUrl.Host, "localhost") || strings.HasPrefix(parsedUrl.Host, "127.0.0.1") { + return true + } + + return false +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosign/auth/auth_test.go juju-core-1.18.0/src/github.com/joyent/gosign/auth/auth_test.go --- juju-core-1.17.7/src/github.com/joyent/gosign/auth/auth_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosign/auth/auth_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,87 @@ +// +// gosign - Go HTTP signing library for the Joyent Public Cloud and Joyent Manta +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package auth_test + +import ( + "net/http" + "testing" + + gc "launchpad.net/gocheck" + + "github.com/joyent/gosign/auth" +) + +const ( + SdcSignature = "yK0J17CQ04ZvMsFLoH163Sjyg8tE4BoIeCsmKWLQKN3BYgSpR0XyqrecheQ2A0o4L99oSumYSKIscBSiH5rqdf4/1zC/FEkYOI2UzcIHYb1MPNzO3g/5X44TppYE+8dxoH99V+Ts8RT3ZurEYjQ8wmK0TnxdirAevSpbypZJaBOFXUZSxx80m5BD4QE/MSGo/eaVdJI/Iw+nardHNvimVCr6pRNycX1I4FdyRR6kgrAl2NkY2yxx/CAY21Ir+dmbG3A1x4GiIE485LLheAL5/toPo7Gh8G5fkrF9dXWVyX0k9AZXqXNWn5AZxc32dKL2enH09j/X86RtwiR1IEuPww==" + MantaSignature = "unBowZ/HOydMxzYkmoB192rn006vujsuZvhx/CieAl+k+YoQsHMM1tAPwbxs71o65sMMymRBZGOZU91lvbEW94rF950HDYy1mhqTf4QAHXc3Km3lInXvAQuvsMrZUofNApzxIdAacNL/ESJ8JCU8sxT2919cDCKkVI8vqOvUJvCyCSIlkBr9d+MLBHuFwr6zRgd3pZSMbMoKrrX6XzsQIUhOldrbSJXYzaQnwvvY2pygPEl491mzY+gt+jiykSVTMlLM2+iCrP4/rmMHenpGYjN2tNftNwo2U6rFwWKwWkK5G1n5YrKMLIt6CV6z+nFLsvhimCtP7WY+pOuVU+1hrA==" + testJpcKeyName = "test_key" + key = `-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEAyLOtVh8qXjdwfjZZYwkEgg1yoSzmpKKpmzYW745lBGtPH87F +spHVHeqjmgFnBsARsD7CHzYyQTho7oLrAEbuF7tKdGRK25wJIenPKKuL+UVwZNeJ +VEXSiMNmX3Y4IqRteqRIjhw3DmXYHEWvBc2JVy8lWtyK+o6o8jlO0aRTTT2+dETp +yqKqNJyHVNz2u6XVtm7jqyLU7tAqW+qpr5zSoNmuUAyz6JDCRnlWvwp1qzuS1LV3 +2HK9yfq8TGriDVPyPRpFRmiRGWGIrIKrmm4sImpoLfuVBITjeh8V3Ee0OCDmTLgY +lTHAmCLFJxaW5Y8b4cTt5pbT7R1iu77RKJo3fwIBIwKCAQEAmtPAOx9bMr0NozE9 +pCuHIn9nDp77EUpIUym57AAiCrkuaP6YgnB/1T/6jL9A2VJWycoDdynO/xzjO6bS +iy9nNuDwSyjMCH+vRgwjdyVAGBD+7rTmSFMewUZHqLpIj8CsOgm0UF7opLT3K8C6 +N60vbyRepS3KTEIqjvkCSfPLO5Sp38KZYXKg0/Abb21WDSEzWonjV8JfOyMfUYhh +7QCE+Nf8s3b+vxskOuCQq1WHoqo8CqXrMYVknkvnQuRFPuaOLEjMbJVqlTb9ns2V +SKxmo46R7fl2dKgMBll+Nec+3Dn2/Iq/qnHq34HF/rhz0uvQDv1w1cSEMjLQaHtH +yZMkgwKBgQDtIAY+yqcmGFYiHQT7V35QbmfeJX1/v9KgpcA7L9Qi6H2LgKPlZu3e +Fc5Pp8C82uIzxuKBbEqauoWAEfP7r2wn1EwoQGMdsY9MpPiScS5iwLKuiSWyyjyf +Snmq+wLwVMYr71ijCuD+Ydm2xGoYeogwkV+QuTOS79s7HGM5tAN9TQKBgQDYrXHR +Nc84Xt+86cWCXJ2rAhHoTMXQQSVXoc75CjBPM2oUH6iguVBM7dPG0ORU14+o7Q7Y +gUvQCV6xoWH05nESHG++sidRifM/HT07M1bSjbMPcFmeAeA0mTFodXfRN6dKyibb +5kHUHgkgsC8qpXZr1KsNR7BcvC+xKuG1qC1R+wKBgDz5mzS3xJTEbeuDzhS+uhSu +rP6b7RI4o+AqnyUpjlIehq7X76FjnEBsAdn377uIvdLMvebEEy8aBRJNwmVKXaPX +gUwt0FgXtyJWTowOeaRdb8Z7CbGht9EwaG3LhGmvZiiOANl303Sc0ZVltOG5G7S3 +qtwSXbgRyqjMyQ7WhI3vAoGBAMYa67f2rtRz/8Kp2Sa7E8+M3Swo719RgTotighD +1GWrWav/sB3rQhpzCsRnNyj/mUn9T2bcnRX58C1gWY9zmpQ3QZhoXnZvf0+ltFNi +I36tcIMk5DixQgQ0Sm4iQalXdGGi4bMbqeaB3HWoZaNVc5XJwPYy6mNqOjuU657F +pcdLAoGBAOQRc5kaZ3APKGHu64DzKh5TOam9J2gpRSD5kF2fIkAeaYWU0bE9PlqV +MUxNRzxbIC16eCKFUoDkErIXGQfIMUOm+aCT/qpoAdXIvuO7H0OYRjMDmbscSDEV +cQYaFsx8Z1KwMVBTwDtiGXhd+82+dKnXxH4bZC+WAKs7L79HqhER +-----END RSA PRIVATE KEY-----` +) + +func Test(t *testing.T) { gc.TestingT(t) } + +type AuthSuite struct { +} + +var _ = gc.Suite(&AuthSuite{}) + +func (s *AuthSuite) TestCreateSdcAuthorizationHeader(c *gc.C) { + headers := make(http.Header) + headers["Date"] = []string{"Mon, 14 Oct 2013 18:49:29 GMT"} + authentication := auth.Auth{User: "test_user", PrivateKey: key, Algorithm: "rsa-sha256"} + credentials := &auth.Credentials{ + UserAuthentication: authentication, + SdcKeyId: "test_key", + SdcEndpoint: auth.Endpoint{URL: "http://gotest.api.joyentcloud.com"}, + } + authHeader, err := auth.CreateAuthorizationHeader(headers, credentials, false) + c.Assert(err, gc.IsNil) + c.Assert(authHeader, gc.Equals, "Signature keyId=\"/test_user/keys/"+testJpcKeyName+"\",algorithm=\"rsa-sha256\" "+SdcSignature) +} + +func (s *AuthSuite) TestCreateMantaAuthorizationHeader(c *gc.C) { + headers := make(http.Header) + headers["Date"] = []string{"Mon, 14 Oct 2013 18:49:29 GMT"} + authentication := auth.Auth{User: "test_user", PrivateKey: key, Algorithm: "rsa-sha256"} + credentials := &auth.Credentials{ + UserAuthentication: authentication, + MantaKeyId: "test_key", + MantaEndpoint: auth.Endpoint{URL: "http://gotest.manta.joyent.com"}, + } + authHeader, err := auth.CreateAuthorizationHeader(headers, credentials, true) + c.Assert(err, gc.IsNil) + c.Assert(authHeader, gc.Equals, "Signature keyId=\"/test_user/keys/"+testJpcKeyName+"\",algorithm=\"rsa-sha256\",signature=\""+MantaSignature+"\"") +} diff -Nru juju-core-1.17.7/src/github.com/joyent/gosign/COPYING juju-core-1.18.0/src/github.com/joyent/gosign/COPYING --- juju-core-1.17.7/src/github.com/joyent/gosign/COPYING 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosign/COPYING 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff -Nru juju-core-1.17.7/src/github.com/joyent/gosign/COPYING.LESSER juju-core-1.18.0/src/github.com/joyent/gosign/COPYING.LESSER --- juju-core-1.17.7/src/github.com/joyent/gosign/COPYING.LESSER 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosign/COPYING.LESSER 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff -Nru juju-core-1.17.7/src/github.com/joyent/gosign/.gitignore juju-core-1.18.0/src/github.com/joyent/gosign/.gitignore --- juju-core-1.17.7/src/github.com/joyent/gosign/.gitignore 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosign/.gitignore 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,26 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe + +# IntelliJ files +.idea +*.iml \ No newline at end of file diff -Nru juju-core-1.17.7/src/github.com/joyent/gosign/gosign.go juju-core-1.18.0/src/github.com/joyent/gosign/gosign.go --- juju-core-1.17.7/src/github.com/joyent/gosign/gosign.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosign/gosign.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,15 @@ +/* +The sign package enables Go programs to create signed requests for +the Joyent Public Cloud and Joyent Manta services. + +The sign package is structured as follow: + + - gosign/auth. This package deals with the authorization and signature of requests. + +Licensed under LGPL v3. + +Copyright (c) 2013 Joyent Inc. +Written by Daniele Stroppa + +*/ +package gosign diff -Nru juju-core-1.17.7/src/github.com/joyent/gosign/gosign_test.go juju-core-1.18.0/src/github.com/joyent/gosign/gosign_test.go --- juju-core-1.17.7/src/github.com/joyent/gosign/gosign_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosign/gosign_test.go 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,24 @@ +// +// gosign - Go HTTP signing library for the Joyent Public Cloud and Joyent Manta +// +// +// Copyright (c) 2013 Joyent Inc. +// +// Written by Daniele Stroppa +// + +package gosign + +import ( + gc "launchpad.net/gocheck" + "testing" +) + +func Test(t *testing.T) { + gc.TestingT(t) +} + +type GoSignTestSuite struct { +} + +var _ = gc.Suite(&GoSignTestSuite{}) diff -Nru juju-core-1.17.7/src/github.com/joyent/gosign/LICENSE juju-core-1.18.0/src/github.com/joyent/gosign/LICENSE --- juju-core-1.17.7/src/github.com/joyent/gosign/LICENSE 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosign/LICENSE 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,15 @@ +GoSign - Go HTTP signing library for the Joyent Public Cloud + +Copyright (c) 2013, Joyent Inc. + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU Lesser General Public License as published by the Free +Software Foundation, either version 3 of the License, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + +See both COPYING and COPYING.LESSER for the full terms of the GNU Lesser +General Public License. diff -Nru juju-core-1.17.7/src/github.com/joyent/gosign/README.md juju-core-1.18.0/src/github.com/joyent/gosign/README.md --- juju-core-1.17.7/src/github.com/joyent/gosign/README.md 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/github.com/joyent/gosign/README.md 2014-04-04 16:56:41.000000000 +0000 @@ -0,0 +1,4 @@ +gosign +====== + +Go HTTP signing library diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/charm/charm.go juju-core-1.18.0/src/launchpad.net/juju-core/charm/charm.go --- juju-core-1.17.7/src/launchpad.net/juju-core/charm/charm.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/charm/charm.go 2014-04-04 16:53:35.000000000 +0000 @@ -30,10 +30,10 @@ return ReadBundle(path) } -// InferRepository returns a charm repository inferred from -// the provided URL. Local URLs will use the provided path. -func InferRepository(curl *URL, localRepoPath string) (repo Repository, err error) { - switch curl.Schema { +// InferRepository returns a charm repository inferred from the provided charm +// reference. Local references will use the provided path. +func InferRepository(ref Reference, localRepoPath string) (repo Repository, err error) { + switch ref.Schema { case "cs": repo = Store case "local": @@ -42,7 +42,7 @@ } repo = &LocalRepository{Path: localRepoPath} default: - return nil, fmt.Errorf("unknown schema for charm URL %q", curl) + return nil, fmt.Errorf("unknown schema for charm reference %q", ref) } return } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/charm/charm_test.go juju-core-1.18.0/src/launchpad.net/juju-core/charm/charm_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/charm/charm_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/charm/charm_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -48,7 +48,7 @@ c.Logf("test %d", i) curl, err := charm.InferURL(t.url, "precise") c.Assert(err, gc.IsNil) - repo, err := charm.InferRepository(curl, "/some/path") + repo, err := charm.InferRepository(curl.Reference, "/some/path") c.Assert(err, gc.IsNil) switch repo := repo.(type) { case *charm.LocalRepository: @@ -59,11 +59,11 @@ } curl, err := charm.InferURL("local:whatever", "precise") c.Assert(err, gc.IsNil) - _, err = charm.InferRepository(curl, "") + _, err = charm.InferRepository(curl.Reference, "") c.Assert(err, gc.ErrorMatches, "path to local repository not specified") curl.Schema = "foo" - _, err = charm.InferRepository(curl, "") - c.Assert(err, gc.ErrorMatches, "unknown schema for charm URL.*") + _, err = charm.InferRepository(curl.Reference, "") + c.Assert(err, gc.ErrorMatches, "unknown schema for charm reference.*") } func checkDummy(c *gc.C, f charm.Charm, path string) { diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/charm/meta.go juju-core-1.18.0/src/launchpad.net/juju-core/charm/meta.go --- juju-core-1.17.7/src/launchpad.net/juju-core/charm/meta.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/charm/meta.go 2014-04-04 16:53:35.000000000 +0000 @@ -102,6 +102,7 @@ Format int `bson:",omitempty"` OldRevision int `bson:",omitempty"` // Obsolete Categories []string `bson:",omitempty"` + Series string `bson:",omitempty"` } func generateRelationHooks(relName string, allHooks map[string]bool) { @@ -179,6 +180,9 @@ // Obsolete meta.OldRevision = int(m["revision"].(int64)) } + if series, ok := m["series"]; ok && series != nil { + meta.Series = series.(string) + } if err := meta.Check(); err != nil { return nil, err } @@ -243,6 +247,13 @@ return fmt.Errorf("subordinate charm %q lacks \"requires\" relation with container scope", meta.Name) } } + + if meta.Series != "" { + if !IsValidSeries(meta.Series) { + return fmt.Errorf("charm %q declares invalid series: %q", meta.Name, meta.Series) + } + } + return nil } @@ -357,6 +368,7 @@ "format": schema.Int(), "subordinate": schema.Bool(), "categories": schema.List(schema.String()), + "series": schema.String(), }, schema.Defaults{ "provides": schema.Omit, @@ -366,5 +378,6 @@ "format": 1, "subordinate": schema.Omit, "categories": schema.Omit, + "series": schema.Omit, }, ) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/charm/meta_test.go juju-core-1.18.0/src/launchpad.net/juju-core/charm/meta_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/charm/meta_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/charm/meta_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -5,6 +5,7 @@ import ( "bytes" + "fmt" "io" "io/ioutil" "os" @@ -239,6 +240,37 @@ innocuous: juju-info`, "") } +// dummyMetadata contains a minimally valid charm metadata.yaml +// for testing valid and invalid series. +const dummyMetadata = "name: a\nsummary: b\ndescription: c" + +// TestSeries ensures that valid series values are parsed correctly when specified +// in the charm metadata. +func (s *MetaSuite) TestSeries(c *gc.C) { + // series not specified + meta, err := charm.ReadMeta(strings.NewReader(dummyMetadata)) + c.Assert(err, gc.IsNil) + c.Check(meta.Series, gc.Equals, "") + + for _, seriesName := range []string{"precise", "trusty", "plan9"} { + meta, err := charm.ReadMeta(strings.NewReader( + fmt.Sprintf("%s\nseries: %s\n", dummyMetadata, seriesName))) + c.Assert(err, gc.IsNil) + c.Check(meta.Series, gc.Equals, seriesName) + } +} + +// TestInvalidSeries ensures that invalid series values cause a parse error +// when specified in the charm metadata. +func (s *MetaSuite) TestInvalidSeries(c *gc.C) { + for _, seriesName := range []string{"pre-c1se", "pre^cise", "cp/m", "OpenVMS"} { + _, err := charm.ReadMeta(strings.NewReader( + fmt.Sprintf("%s\nseries: %s\n", dummyMetadata, seriesName))) + c.Assert(err, gc.NotNil) + c.Check(err, gc.ErrorMatches, `charm "a" declares invalid series: .*`) + } +} + func (s *MetaSuite) TestCheckMismatchedRelationName(c *gc.C) { // This Check case cannot be covered by the above // TestRelationsConstraints tests. diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/charm/url.go juju-core-1.18.0/src/launchpad.net/juju-core/charm/url.go --- juju-core-1.17.7/src/launchpad.net/juju-core/charm/url.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/charm/url.go 2014-04-04 16:53:35.000000000 +0000 @@ -284,8 +284,6 @@ return nil } -var jsonNull = []byte("null") - func (u *URL) MarshalJSON() ([]byte, error) { if u == nil { panic("cannot marshal nil *charm.URL") @@ -306,6 +304,23 @@ return nil } +func (r *Reference) MarshalJSON() ([]byte, error) { + return json.Marshal(r.String()) +} + +func (r *Reference) UnmarshalJSON(b []byte) error { + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + ref, _, err := ParseReference(s) + if err != nil { + return err + } + *r = ref + return nil +} + // Quote translates a charm url string into one which can be safely used // in a file path. ASCII letters, ASCII digits, dot and dash stay the // same; other characters are translated to their hex representation diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/charm/url_test.go juju-core-1.18.0/src/launchpad.net/juju-core/charm/url_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/charm/url_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/charm/url_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -258,7 +258,7 @@ Unmarshal: json.Unmarshal, }} -func (s *URLSuite) TestCodecs(c *gc.C) { +func (s *URLSuite) TestURLCodecs(c *gc.C) { for i, codec := range codecs { c.Logf("codec %d", i) type doc struct { @@ -279,6 +279,25 @@ } } +func (s *URLSuite) TestReferenceJSON(c *gc.C) { + ref, _, err := charm.ParseReference("cs:series/name") + c.Assert(err, gc.IsNil) + data, err := json.Marshal(&ref) + c.Assert(err, gc.IsNil) + c.Check(string(data), gc.Equals, `"cs:name"`) + + var parsed charm.Reference + err = json.Unmarshal(data, &parsed) + c.Assert(err, gc.IsNil) + c.Check(parsed, gc.DeepEquals, ref) + + // unmarshalling json gibberish and invalid charm reference strings + for _, value := range []string{":{", `"cs:{}+<"`, `"cs:~_~/f00^^&^/baaaar$%-?"`} { + err = json.Unmarshal([]byte(value), &parsed) + c.Check(err, gc.NotNil) + } +} + type QuoteSuite struct{} var _ = gc.Suite(&QuoteSuite{}) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/addmachine.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/addmachine.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/addmachine.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/addmachine.go 2014-04-04 16:57:31.000000000 +0000 @@ -11,6 +11,7 @@ "launchpad.net/juju-core/cmd" "launchpad.net/juju-core/constraints" + "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/environs/manual" "launchpad.net/juju-core/instance" "launchpad.net/juju-core/juju" @@ -126,7 +127,7 @@ if err != nil { return "", err } - series = conf.DefaultSeries() + series = config.PreferredSeries(conf) } template := state.MachineTemplate{ Series: series, diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/bootstrap_test.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/bootstrap_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/bootstrap_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/bootstrap_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -15,6 +15,7 @@ "launchpad.net/juju-core/cmd" "launchpad.net/juju-core/constraints" "launchpad.net/juju-core/environs" + "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/environs/configstore" "launchpad.net/juju-core/environs/filestorage" "launchpad.net/juju-core/environs/imagemetadata" @@ -119,7 +120,8 @@ func (s *BootstrapSuite) runAllowRetriesTest(c *gc.C, test bootstrapRetryTest) { toolsVersions := envtesting.VAll if test.version != "" { - testVersion := version.MustParseBinary(test.version) + useVersion := strings.Replace(test.version, "%LTS%", config.LatestLtsSeries(), 1) + testVersion := version.MustParseBinary(useVersion) s.PatchValue(&version.Current, testVersion) if test.addVersionToSource { toolsVersions = append([]version.Binary{}, toolsVersions...) @@ -201,8 +203,9 @@ defer fake.Restore() if test.version != "" { + useVersion := strings.Replace(test.version, "%LTS%", config.LatestLtsSeries(), 1) origVersion := version.Current - version.Current = version.MustParseBinary(test.version) + version.Current = version.MustParseBinary(useVersion) defer func() { version.Current = origVersion }() } @@ -217,7 +220,7 @@ uploadCount := len(test.uploads) if uploadCount == 0 { usefulVersion := version.Current - usefulVersion.Series = env.Config().DefaultSeries() + usefulVersion.Series = config.PreferredSeries(env.Config()) envtesting.AssertUploadFakeToolsVersions(c, env.Storage(), usefulVersion) } @@ -245,6 +248,7 @@ urls := list.URLs() c.Check(urls, gc.HasLen, len(test.uploads)) for _, v := range test.uploads { + v := strings.Replace(v, "%LTS%", config.LatestLtsSeries(), 1) c.Logf("seeking: " + v) vers := version.MustParseBinary(v) _, found := urls[vers] @@ -300,7 +304,7 @@ err: `--series requires --upload-tools`, }, { info: "bad environment", - version: "1.2.3-precise-amd64", + version: "1.2.3-%LTS%-amd64", args: []string{"-e", "brokenenv"}, err: `dummy.Bootstrap is broken`, }, { @@ -312,9 +316,9 @@ version: "1.2.3-saucy-amd64", args: []string{"--upload-tools"}, uploads: []string{ - "1.2.3.1-saucy-amd64", // from version.Current - "1.2.3.1-raring-amd64", // from env.Config().DefaultSeries() - "1.2.3.1-precise-amd64", // from environs/config.DefaultSeries + "1.2.3.1-saucy-amd64", // from version.Current + "1.2.3.1-raring-amd64", // from env.Config().DefaultSeries() + "1.2.3.1-%LTS%-amd64", // from environs/config.DefaultSeries }, }, { info: "--upload-tools uses arch from constraint if it matches current version", @@ -322,18 +326,18 @@ hostArch: "ppc64", args: []string{"--upload-tools", "--constraints", "arch=ppc64"}, uploads: []string{ - "1.3.3.1-saucy-ppc64", // from version.Current - "1.3.3.1-raring-ppc64", // from env.Config().DefaultSeries() - "1.3.3.1-precise-ppc64", // from environs/config.DefaultSeries + "1.3.3.1-saucy-ppc64", // from version.Current + "1.3.3.1-raring-ppc64", // from env.Config().DefaultSeries() + "1.3.3.1-%LTS%-ppc64", // from environs/config.DefaultSeries }, constraints: constraints.MustParse("arch=ppc64"), }, { info: "--upload-tools only uploads each file once", - version: "1.2.3-precise-amd64", + version: "1.2.3-%LTS%-amd64", args: []string{"--upload-tools"}, uploads: []string{ "1.2.3.1-raring-amd64", - "1.2.3.1-precise-amd64", + "1.2.3.1-%LTS%-amd64", }, }, { info: "--upload-tools rejects invalid series", @@ -357,7 +361,7 @@ args: []string{"--upload-tools"}, uploads: []string{ "1.2.3.5-raring-amd64", - "1.2.3.5-precise-amd64", + "1.2.3.5-%LTS%-amd64", }, }} @@ -365,7 +369,12 @@ env, fake := makeEmptyFakeHome(c) defer fake.Restore() defaultSeriesVersion := version.Current - defaultSeriesVersion.Series = env.Config().DefaultSeries() + defaultSeriesVersion.Series = config.PreferredSeries(env.Config()) + // Force a dev version by having an odd minor version number. + // This is because we have not uploaded any tools and auto + // upload is only enabled for dev versions. + defaultSeriesVersion.Minor = 11 + s.PatchValue(&version.Current, defaultSeriesVersion) ctx := coretesting.Context(c) code := cmd.Main(&BootstrapCommand{}, ctx, nil) @@ -384,7 +393,12 @@ env, fake := makeEmptyFakeHome(c) defer fake.Restore() defaultSeriesVersion := version.Current - defaultSeriesVersion.Series = env.Config().DefaultSeries() + defaultSeriesVersion.Series = config.PreferredSeries(env.Config()) + // Force a dev version by having an odd minor version number. + // This is because we have not uploaded any tools and auto + // upload is only enabled for dev versions. + defaultSeriesVersion.Minor = 11 + s.PatchValue(&version.Current, defaultSeriesVersion) store, err := configstore.Default() c.Assert(err, gc.IsNil) @@ -455,6 +469,12 @@ defer fake.Restore() // Bootstrap the environment with the valid source. + // Force a dev version by having an odd minor version number. + // This is because we have not uploaded any tools and auto + // upload is only enabled for dev versions. + devVersion := version.Current + devVersion.Minor = 11 + s.PatchValue(&version.Current, devVersion) ctx := coretesting.Context(c) code := cmd.Main(&BootstrapCommand{}, ctx, []string{"--metadata-source", sourceDir}) c.Check(code, gc.Equals, 0) @@ -516,7 +536,13 @@ c.Assert(err, gc.IsNil) c.Logf("found: " + list.String()) urls := list.URLs() - c.Assert(urls, gc.HasLen, 2) + expectedUrlCount := 2 + + // There will be distinct tools for each of these if they are different + if config.LatestLtsSeries() != coretesting.FakeDefaultSeries { + expectedUrlCount++ + } + c.Assert(urls, gc.HasLen, expectedUrlCount) expectedVers := []version.Binary{ version.MustParseBinary(fmt.Sprintf("1.7.3.1-%s-%s", otherSeries, version.Current.Arch)), version.MustParseBinary(fmt.Sprintf("1.7.3.1-%s-%s", version.Current.Series, version.Current.Arch)), @@ -558,7 +584,11 @@ code := cmd.Main(&BootstrapCommand{}, context, nil) c.Assert(code, gc.Equals, 1) errText := context.Stderr.(*bytes.Buffer).String() - expectedErrText := "uploading tools for series \\[precise raring\\]\n" + expectedErrText := "uploading tools for series \\[precise " + if config.LatestLtsSeries() != coretesting.FakeDefaultSeries { + expectedErrText += config.LatestLtsSeries() + " " + } + expectedErrText += "raring\\]\n" expectedErrText += "Bootstrap failed, destroying environment\n" expectedErrText += "error: cannot upload bootstrap tools: an error\n" c.Assert(errText, gc.Matches, expectedErrText) @@ -567,6 +597,12 @@ func (s *BootstrapSuite) TestBootstrapDestroy(c *gc.C) { _, fake := makeEmptyFakeHome(c) defer fake.Restore() + devVersion := version.Current + // Force a dev version by having an odd minor version number. + // This is because we have not uploaded any tools and auto + // upload is only enabled for dev versions. + devVersion.Minor = 11 + s.PatchValue(&version.Current, devVersion) opc, errc := runCommand(nullContext(c), new(BootstrapCommand), "-e", "brokenenv") err := <-errc c.Assert(err, gc.ErrorMatches, "dummy.Bootstrap is broken") diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/common.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/common.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/common.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/common.go 2014-04-04 16:53:35.000000000 +0000 @@ -4,10 +4,13 @@ package main import ( + "launchpad.net/juju-core/charm" "launchpad.net/juju-core/cmd" "launchpad.net/juju-core/environs" + "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/environs/configstore" "launchpad.net/juju-core/errors" + "launchpad.net/juju-core/state/api" ) // destroyPreparedEnviron destroys the environment and logs an error if it fails. @@ -45,3 +48,39 @@ } return environ, cleanup, nil } + +// resolveCharmURL returns a resolved charm URL, given a charm location string. +// If the series is not resolved, the environment default-series is used, or if +// not set, the series is resolved with the state server. +func resolveCharmURL(url string, client *api.Client, conf *config.Config) (*charm.URL, error) { + ref, series, err := charm.ParseReference(url) + if err != nil { + return nil, err + } + // If series is not set, use configured default series + if series == "" { + if defaultSeries, ok := conf.DefaultSeries(); ok { + series = defaultSeries + } + } + // Otherwise, look up the best supported series for this charm + if series == "" { + return client.ResolveCharm(ref) + } + return &charm.URL{Reference: ref, Series: series}, nil +} + +// resolveCharmURL1dot16 returns a resolved charm URL for older state servers +// that do not support ResolveCharm. The default series "precise" is +// appropriate for these environments. +func resolveCharmURL1dot16(url string, conf *config.Config) (*charm.URL, error) { + ref, series, err := charm.ParseReference(url) + if err != nil { + return nil, err + } + + if series == "" { + series = config.PreferredSeries(conf) + } + return &charm.URL{Reference: ref, Series: series}, err +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/deploy.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/deploy.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/deploy.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/deploy.go 2014-04-04 16:57:31.000000000 +0000 @@ -133,11 +133,13 @@ if err != nil { return err } - curl, err := charm.InferURL(c.CharmName, conf.DefaultSeries()) + + curl, err := resolveCharmURL(c.CharmName, client, conf) if err != nil { return err } - repo, err := charm.InferRepository(curl, ctx.AbsPath(c.RepoPath)) + + repo, err := charm.InferRepository(curl.Reference, ctx.AbsPath(c.RepoPath)) if err != nil { return err } @@ -204,15 +206,16 @@ if err != nil { return err } - curl, err := charm.InferURL(c.CharmName, conf.DefaultSeries()) + + curl, err := resolveCharmURL1dot16(c.CharmName, conf) if err != nil { return err } - repo, err := charm.InferRepository(curl, ctx.AbsPath(c.RepoPath)) + + repo, err := charm.InferRepository(curl.Reference, c.RepoPath) if err != nil { return err } - repo = config.SpecializeCharmRepo(repo, conf) // TODO(fwereade) it's annoying to roundtrip the bytes through the client diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/environment_test.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/environment_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/environment_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/environment_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -124,12 +124,22 @@ } func (s *SetEnvironmentSuite) TestChangeDefaultSeries(c *gc.C) { - _, err := testing.RunCommand(c, &SetEnvironmentCommand{}, []string{"default-series=raring"}) + // default-series not set + stateConfig, err := s.State.EnvironConfig() c.Assert(err, gc.IsNil) + series, ok := stateConfig.DefaultSeries() + c.Assert(ok, gc.Equals, true) + c.Assert(series, gc.Equals, "precise") // default-series set in RepoSuite.SetUpTest - stateConfig, err := s.State.EnvironConfig() + _, err = testing.RunCommand(c, &SetEnvironmentCommand{}, []string{"default-series=raring"}) + c.Assert(err, gc.IsNil) + + stateConfig, err = s.State.EnvironConfig() c.Assert(err, gc.IsNil) - c.Assert(stateConfig.DefaultSeries(), gc.Equals, "raring") + series, ok = stateConfig.DefaultSeries() + c.Assert(ok, gc.Equals, true) + c.Assert(series, gc.Equals, "raring") + c.Assert(config.PreferredSeries(stateConfig), gc.Equals, "raring") } func (s *SetEnvironmentSuite) TestChangeBooleanAttribute(c *gc.C) { diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/publish.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/publish.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/publish.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/publish.go 2014-04-04 16:57:31.000000000 +0000 @@ -110,7 +110,7 @@ pushLocation = c.changePushLocation(pushLocation) } - repo, err := charm.InferRepository(curl, "/not/important") + repo, err := charm.InferRepository(curl.Reference, "/not/important") if err != nil { return err } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/status_test.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/status_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/status_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/status_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -1047,7 +1047,8 @@ "agent-state": "started", "subordinates": M{ "logging/0": M{ - "agent-state": "started", + "agent-state": "started", + "public-address": "dummyenv-1.dns", }, }, "public-address": "dummyenv-1.dns", @@ -1069,6 +1070,7 @@ "logging/1": M{ "agent-state": "error", "agent-state-info": "somehow lost in all those logs", + "public-address": "dummyenv-2.dns", }, }, "public-address": "dummyenv-2.dns", @@ -1112,7 +1114,8 @@ "agent-state": "started", "subordinates": M{ "logging/0": M{ - "agent-state": "started", + "agent-state": "started", + "public-address": "dummyenv-1.dns", }, }, "public-address": "dummyenv-1.dns", @@ -1134,6 +1137,7 @@ "logging/1": M{ "agent-state": "error", "agent-state-info": "somehow lost in all those logs", + "public-address": "dummyenv-2.dns", }, }, "public-address": "dummyenv-2.dns", @@ -1176,7 +1180,8 @@ "agent-state": "started", "subordinates": M{ "logging/0": M{ - "agent-state": "started", + "agent-state": "started", + "public-address": "dummyenv-1.dns", }, }, "public-address": "dummyenv-1.dns", @@ -1253,7 +1258,8 @@ "agent-state": "started", "subordinates": M{ "monitoring/0": M{ - "agent-state": "started", + "agent-state": "started", + "public-address": "dummyenv-1.dns", }, }, "public-address": "dummyenv-1.dns", diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/upgradecharm.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/upgradecharm.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/upgradecharm.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/upgradecharm.go 2014-04-04 16:57:31.000000000 +0000 @@ -129,8 +129,7 @@ var newURL *charm.URL if c.SwitchURL != "" { - // A new charm URL was explicitly specified. - newURL, err = charm.InferURL(c.SwitchURL, conf.DefaultSeries()) + newURL, err = resolveCharmURL(c.SwitchURL, client, conf) if err != nil { return err } @@ -138,11 +137,11 @@ // No new URL specified, but revision might have been. newURL = oldURL.WithRevision(c.Revision) } - repo, err := charm.InferRepository(newURL, ctx.AbsPath(c.RepoPath)) + + repo, err := charm.InferRepository(newURL.Reference, ctx.AbsPath(c.RepoPath)) if err != nil { return err } - repo = config.SpecializeCharmRepo(repo, conf) // If no explicit revision was set with either SwitchURL @@ -198,11 +197,7 @@ var newURL *charm.URL if c.SwitchURL != "" { // A new charm URL was explicitly specified. - conf, err := conn.State.EnvironConfig() - if err != nil { - return err - } - newURL, err = charm.InferURL(c.SwitchURL, conf.DefaultSeries()) + newURL, err = resolveCharmURL1dot16(c.SwitchURL, conf) if err != nil { return err } @@ -210,11 +205,11 @@ // No new URL specified, but revision might have been. newURL = oldURL.WithRevision(c.Revision) } - repo, err := charm.InferRepository(newURL, ctx.AbsPath(c.RepoPath)) + + repo, err := charm.InferRepository(newURL.Reference, ctx.AbsPath(c.RepoPath)) if err != nil { return err } - repo = config.SpecializeCharmRepo(repo, conf) // If no explicit revision was set with either SwitchURL diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/upgradejuju_test.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/upgradejuju_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/juju/upgradejuju_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/juju/upgradejuju_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -14,6 +14,7 @@ jc "github.com/juju/testing/checkers" gc "launchpad.net/gocheck" + "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/environs/filestorage" "launchpad.net/juju-core/environs/storage" "launchpad.net/juju-core/environs/sync" @@ -225,14 +226,14 @@ agentVersion: "2.0.0", args: []string{"--upload-tools"}, expectVersion: "2.2.0.1", - expectUploaded: []string{"2.2.0.1-quantal-amd64", "2.2.0.1-precise-amd64", "2.2.0.1-raring-amd64"}, + expectUploaded: []string{"2.2.0.1-quantal-amd64", "2.2.0.1-%LTS%-amd64", "2.2.0.1-raring-amd64"}, }, { about: "upload with explicit version", currentVersion: "2.2.0-quantal-amd64", agentVersion: "2.0.0", args: []string{"--upload-tools", "--version", "2.7.3"}, expectVersion: "2.7.3.1", - expectUploaded: []string{"2.7.3.1-quantal-amd64", "2.7.3.1-precise-amd64", "2.7.3.1-raring-amd64"}, + expectUploaded: []string{"2.7.3.1-quantal-amd64", "2.7.3.1-%LTS%-amd64", "2.7.3.1-raring-amd64"}, }, { about: "upload with explicit series", currentVersion: "2.2.0-quantal-amd64", @@ -246,7 +247,7 @@ agentVersion: "2.0.0", args: []string{"--upload-tools"}, expectVersion: "2.1.0.1", - expectUploaded: []string{"2.1.0.1-quantal-amd64", "2.1.0.1-precise-amd64", "2.1.0.1-raring-amd64"}, + expectUploaded: []string{"2.1.0.1-quantal-amd64", "2.1.0.1-%LTS%-amd64", "2.1.0.1-raring-amd64"}, }, { about: "upload bumps version when necessary", tools: []string{"2.4.6-quantal-amd64", "2.4.8-quantal-amd64"}, @@ -254,7 +255,7 @@ agentVersion: "2.4.0", args: []string{"--upload-tools"}, expectVersion: "2.4.6.1", - expectUploaded: []string{"2.4.6.1-quantal-amd64", "2.4.6.1-precise-amd64", "2.4.6.1-raring-amd64"}, + expectUploaded: []string{"2.4.6.1-quantal-amd64", "2.4.6.1-%LTS%-amd64", "2.4.6.1-raring-amd64"}, }, { about: "upload re-bumps version when necessary", tools: []string{"2.4.6-quantal-amd64", "2.4.6.2-saucy-i386", "2.4.8-quantal-amd64"}, @@ -262,7 +263,7 @@ agentVersion: "2.4.6.2", args: []string{"--upload-tools"}, expectVersion: "2.4.6.3", - expectUploaded: []string{"2.4.6.3-quantal-amd64", "2.4.6.3-precise-amd64", "2.4.6.3-raring-amd64"}, + expectUploaded: []string{"2.4.6.3-quantal-amd64", "2.4.6.3-%LTS%-amd64", "2.4.6.3-raring-amd64"}, }, { about: "upload with explicit version bumps when necessary", currentVersion: "2.2.0-quantal-amd64", @@ -270,7 +271,7 @@ agentVersion: "2.0.0", args: []string{"--upload-tools", "--version", "2.7.3"}, expectVersion: "2.7.3.2", - expectUploaded: []string{"2.7.3.2-quantal-amd64", "2.7.3.2-precise-amd64", "2.7.3.2-raring-amd64"}, + expectUploaded: []string{"2.7.3.2-quantal-amd64", "2.7.3.2-%LTS%-amd64", "2.7.3.2-raring-amd64"}, }} // getMockBuildTools returns a sync.BuildToolsTarballFunc implementation which generates @@ -356,6 +357,9 @@ c.Check(agentVersion, gc.Equals, version.MustParse(test.expectVersion)) for _, uploaded := range test.expectUploaded { + // Substitute latest LTS for placeholder in expected series for uploaded tools + uploaded = strings.Replace(uploaded, "%LTS%", config.LatestLtsSeries(), 1) + vers := version.MustParseBinary(uploaded) r, err := storage.Get(s.Conn.Environ.Storage(), envtools.StorageName(vers)) if !c.Check(err, gc.IsNil) { diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/imagemetadata.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/imagemetadata.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/imagemetadata.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/imagemetadata.go 2014-04-04 16:57:31.000000000 +0000 @@ -96,7 +96,7 @@ } cfg := environ.Config() if c.Series == "" { - c.Series = cfg.DefaultSeries() + c.Series = config.PreferredSeries(cfg) } if v, ok := cfg.AllAttrs()["control-bucket"]; ok { c.privateStorage = v.(string) @@ -109,7 +109,7 @@ logger.Infof("no environment found, creating image metadata using user supplied data") } if c.Series == "" { - c.Series = config.DefaultSeries + c.Series = config.LatestLtsSeries() } if c.ImageId == "" { return fmt.Errorf("image id must be specified") diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/imagemetadata_test.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/imagemetadata_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/imagemetadata_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/imagemetadata_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -15,6 +15,7 @@ gc "launchpad.net/gocheck" "launchpad.net/juju-core/cmd" + "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/testing" "launchpad.net/juju-core/testing/testbase" ) @@ -61,6 +62,7 @@ var seriesVersions map[string]string = map[string]string{ "precise": "12.04", "raring": "13.04", + "trusty": "14.04", } type expectedMetadata struct { @@ -138,7 +140,7 @@ s.assertCommandOutput(c, expected, out, defaultIndexFileName, defaultImageFileName) } -func (s *ImageMetadataSuite) TestImageMetadataFilesDefaultSeries(c *gc.C) { +func (s *ImageMetadataSuite) TestImageMetadataFilesLatestLts(c *gc.C) { ctx := testing.Context(c) code := cmd.Main( &ImageMetadataCommand{}, ctx, []string{ @@ -146,7 +148,7 @@ c.Assert(code, gc.Equals, 0) out := testing.Stdout(ctx) expected := expectedMetadata{ - series: "precise", + series: config.LatestLtsSeries(), arch: "arch", } s.assertCommandOutput(c, expected, out, defaultIndexFileName, defaultImageFileName) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/toolsmetadata_test.go juju-core-1.18.0/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/toolsmetadata_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/toolsmetadata_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/cmd/plugins/juju-metadata/toolsmetadata_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -57,7 +57,7 @@ var currentVersionStrings = []string{ // only these ones will make it into the JSON files. version.Current.Number.String() + "-quantal-amd64", - version.Current.Number.String() + "-quantal-arm", + version.Current.Number.String() + "-quantal-armhf", version.Current.Number.String() + "-quantal-i386", } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/constraints/constraints_test.go juju-core-1.18.0/src/launchpad.net/juju-core/constraints/constraints_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/constraints/constraints_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/constraints/constraints_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -83,8 +83,8 @@ summary: "set arch i386", args: []string{"arch=i386"}, }, { - summary: "set arch arm", - args: []string{"arch=arm"}, + summary: "set arch armhf", + args: []string{"arch=armhf"}, }, { summary: "set nonsense arch 1", args: []string{"arch=cheese"}, @@ -99,7 +99,7 @@ err: `bad "arch" constraint: already set`, }, { summary: "double set arch separately", - args: []string{"arch=arm", "arch="}, + args: []string{"arch=armhf", "arch="}, err: `bad "arch" constraint: already set`, }, @@ -269,7 +269,7 @@ args: []string{" root-disk=8G mem=2T arch=i386 cpu-cores=4096 cpu-power=9001 container=lxc tags=foo,bar"}, }, { summary: "kitchen sink separately", - args: []string{"root-disk=8G", "mem=2T", "cpu-cores=4096", "cpu-power=9001", "arch=arm", "container=lxc", "tags=foo,bar"}, + args: []string{"root-disk=8G", "mem=2T", "cpu-cores=4096", "cpu-power=9001", "arch=armhf", "container=lxc", "tags=foo,bar"}, }, } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/container/kvm/kvm_test.go juju-core-1.18.0/src/launchpad.net/juju-core/container/kvm/kvm_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/container/kvm/kvm_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/container/kvm/kvm_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -169,14 +169,14 @@ RootDisk: 4, }, }, { - cons: "arch=arm", + cons: "arch=armhf", expected: kvm.StartParams{ Memory: kvm.DefaultMemory, CpuCores: kvm.DefaultCpu, RootDisk: kvm.DefaultDisk, }, infoLog: []string{ - `arch constraint of "arm" being ignored as not supported`, + `arch constraint of "armhf" being ignored as not supported`, }, }, { cons: "container=lxc", @@ -209,14 +209,14 @@ `tags constraint of "foo,bar" being ignored as not supported`, }, }, { - cons: "mem=4G cpu-cores=4 root-disk=20G arch=arm cpu-power=100 container=lxc tags=foo,bar", + cons: "mem=4G cpu-cores=4 root-disk=20G arch=armhf cpu-power=100 container=lxc tags=foo,bar", expected: kvm.StartParams{ Memory: 4 * 1024, CpuCores: 4, RootDisk: 20, }, infoLog: []string{ - `arch constraint of "arm" being ignored as not supported`, + `arch constraint of "armhf" being ignored as not supported`, `container constraint of "lxc" being ignored as not supported`, `cpu-power constraint of 100 being ignored as not supported`, `tags constraint of "foo,bar" being ignored as not supported`, diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/dependencies.tsv juju-core-1.18.0/src/launchpad.net/juju-core/dependencies.tsv --- juju-core-1.17.7/src/launchpad.net/juju-core/dependencies.tsv 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/dependencies.tsv 2014-04-04 16:57:31.000000000 +0000 @@ -1,6 +1,10 @@ code.google.com/p/go.crypto hg 6478cc9340cbbe6c04511280c5007722269108e9 184 code.google.com/p/go.net hg 3591c18acabc99439c783463ef00e6dc277eee39 77 github.com/errgo/errgo git 93d72bf813883d1054cae1c001d3a46603f7f559 +github.com/joyent/gocommon git 98b151a080efe19bcde223d2d3b04389963d2347 +github.com/joyent/gomanta git ff785814c0ebb4050420a2f1d47895b35b8808f2 +github.com/joyent/gosdc git 10bbe92c5d98c8b38a0b7f62ee042c7252150efc +github.com/joyent/gosign git 476720af5427223da5420afbbadf620bfb760345 github.com/juju/loggo git fa3acf9ab9ed09aea29030558528e24a254d27af github.com/juju/ratelimit git 0025ab75db6c6eaa4ffff0240c2c9e617ad1a0eb github.com/juju/testing git 9c0e0686136637876ae659e9056897575236e11f diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/environs/bootstrap/bootstrap_test.go juju-core-1.18.0/src/launchpad.net/juju-core/environs/bootstrap/bootstrap_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/environs/bootstrap/bootstrap_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/environs/bootstrap/bootstrap_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -88,7 +88,7 @@ func uploadTools(c *gc.C, env environs.Environ) { usefulVersion := version.Current - usefulVersion.Series = env.Config().DefaultSeries() + usefulVersion.Series = config.PreferredSeries(env.Config()) envtesting.AssertUploadFakeToolsVersions(c, env.Storage(), usefulVersion) } @@ -180,8 +180,11 @@ } err = bootstrap.Bootstrap(coretesting.Context(c), env, cons) if test.Err != "" { - stripped := strings.Replace(err.Error(), "\n", "", -1) - c.Check(stripped, gc.Matches, ".*"+stripped) + c.Check(err, gc.NotNil) + if err != nil { + stripped := strings.Replace(err.Error(), "\n", "", -1) + c.Check(stripped, gc.Matches, ".*"+stripped) + } continue } else { c.Check(err, gc.IsNil) @@ -213,11 +216,17 @@ s.PatchValue(&arch.HostArch, func() string { return "amd64" }) + // Force a dev version by having an odd minor version number. + // This is because we have not uploaded any tools and auto + // upload is only enabled for dev versions. + devVersion := version.Current + devVersion.Minor = 11 + s.PatchValue(&version.Current, devVersion) env := newEnviron("foo", useDefaultKeys, nil) s.setDummyStorage(c, env) envtesting.RemoveFakeTools(c, env.Storage()) arch := "ppc64" - _, err := bootstrap.EnsureToolsAvailability(coretesting.Context(c), env, env.Config().DefaultSeries(), &arch) + _, err := bootstrap.EnsureToolsAvailability(coretesting.Context(c), env, config.PreferredSeries(env.Config()), &arch) c.Assert(err, gc.NotNil) stripped := strings.Replace(err.Error(), "\n", "", -1) c.Assert(stripped, @@ -230,10 +239,16 @@ s.PatchValue(&arch.HostArch, func() string { return "ppc64" }) + // Force a dev version by having an odd minor version number. + // This is because we have not uploaded any tools and auto + // upload is only enabled for dev versions. + devVersion := version.Current + devVersion.Minor = 11 + s.PatchValue(&version.Current, devVersion) env := newEnviron("foo", useDefaultKeys, nil) s.setDummyStorage(c, env) envtesting.RemoveFakeTools(c, env.Storage()) - _, err := bootstrap.EnsureToolsAvailability(coretesting.Context(c), env, env.Config().DefaultSeries(), nil) + _, err := bootstrap.EnsureToolsAvailability(coretesting.Context(c), env, config.PreferredSeries(env.Config()), nil) c.Assert(err, gc.NotNil) stripped := strings.Replace(err.Error(), "\n", "", -1) c.Assert(stripped, @@ -246,7 +261,7 @@ env := newEnviron("foo", useDefaultKeys, map[string]interface{}{"agent-version": "1.16.0"}) s.setDummyStorage(c, env) envtesting.RemoveFakeTools(c, env.Storage()) - _, err := bootstrap.EnsureToolsAvailability(coretesting.Context(c), env, env.Config().DefaultSeries(), nil) + _, err := bootstrap.EnsureToolsAvailability(coretesting.Context(c), env, config.PreferredSeries(env.Config()), nil) c.Assert(err, gc.NotNil) stripped := strings.Replace(err.Error(), "\n", "", -1) c.Assert(stripped, @@ -255,12 +270,12 @@ } func (s *bootstrapSuite) TestEnsureToolsAvailabilityNonDevVersion(c *gc.C) { - // Can't upload tools for released versions. + // Can't automatically upload tools for released versions. s.PatchValue(&version.Current, version.MustParseBinary("1.18.0-trusty-arm64")) env := newEnviron("foo", useDefaultKeys, nil) s.setDummyStorage(c, env) envtesting.RemoveFakeTools(c, env.Storage()) - _, err := bootstrap.EnsureToolsAvailability(coretesting.Context(c), env, env.Config().DefaultSeries(), nil) + _, err := bootstrap.EnsureToolsAvailability(coretesting.Context(c), env, config.PreferredSeries(env.Config()), nil) c.Assert(err, gc.NotNil) stripped := strings.Replace(err.Error(), "\n", "", -1) c.Assert(stripped, @@ -310,12 +325,12 @@ return "arm64" }) arch := "arm64" - agentTools, err := bootstrap.EnsureToolsAvailability(coretesting.Context(c), env, env.Config().DefaultSeries(), &arch) + agentTools, err := bootstrap.EnsureToolsAvailability(coretesting.Context(c), env, config.PreferredSeries(env.Config()), &arch) c.Assert(err, gc.IsNil) c.Assert(agentTools, gc.HasLen, 1) expectedVers := version.Current expectedVers.Number.Build++ - expectedVers.Series = env.Config().DefaultSeries() + expectedVers.Series = config.PreferredSeries(env.Config()) c.Assert(agentTools[0].Version, gc.DeepEquals, expectedVers) } @@ -325,14 +340,21 @@ s.PatchValue(&version.Current, vers) env := newEnviron("foo", useDefaultKeys, nil) cfg := env.Config() - c.Assert(bootstrap.SeriesToUpload(cfg, nil), jc.SameContents, []string{"quantal", "precise"}) + + prefSeries := config.PreferredSeries(cfg) + expect := []string{"quantal", prefSeries} + if prefSeries != config.LatestLtsSeries() { + expect = append(expect, config.LatestLtsSeries()) + } + c.Assert(bootstrap.SeriesToUpload(cfg, nil), jc.SameContents, expect) + c.Assert(bootstrap.SeriesToUpload(cfg, []string{"quantal"}), jc.SameContents, []string{"quantal"}) env = newEnviron("foo", useDefaultKeys, map[string]interface{}{"default-series": "lucid"}) cfg = env.Config() - c.Assert(bootstrap.SeriesToUpload(cfg, nil), jc.SameContents, []string{"quantal", "precise", "lucid"}) + c.Assert(bootstrap.SeriesToUpload(cfg, nil), jc.SameContents, []string{"quantal", config.LatestLtsSeries(), "lucid"}) } -func (s *bootstrapSuite) assertUploadTools(c *gc.C, vers version.Binary, allowRelease bool, +func (s *bootstrapSuite) assertUploadTools(c *gc.C, vers version.Binary, forceVersion bool, extraConfig map[string]interface{}, errMessage string) { s.PatchValue(&version.Current, vers) @@ -353,8 +375,9 @@ return "arm64" }) arch := "arm64" - err := bootstrap.UploadTools(coretesting.Context(c), env, &arch, allowRelease, "precise") + err := bootstrap.UploadTools(coretesting.Context(c), env, &arch, forceVersion, "precise") if errMessage != "" { + c.Assert(err, gc.NotNil) stripped := strings.Replace(err.Error(), "\n", "", -1) c.Assert(stripped, gc.Matches, errMessage) return @@ -362,14 +385,14 @@ c.Assert(err, gc.IsNil) params := envtools.BootstrapToolsParams{ Arch: &arch, - Series: "precise", + Series: version.Current.Series, } agentTools, err := envtools.FindBootstrapTools(env, params) c.Assert(err, gc.IsNil) c.Assert(agentTools, gc.HasLen, 1) expectedVers := vers expectedVers.Number.Build++ - expectedVers.Series = "precise" + expectedVers.Series = version.Current.Series c.Assert(agentTools[0].Version, gc.DeepEquals, expectedVers) } @@ -378,10 +401,10 @@ s.assertUploadTools(c, vers, false, nil, "") } -func (s *bootstrapSuite) TestUploadToolsForceVersionAllowsReleaseTools(c *gc.C) { +func (s *bootstrapSuite) TestUploadToolsReleaseToolsWithDevConfig(c *gc.C) { vers := version.MustParseBinary("1.18.0-trusty-arm64") extraCfg := map[string]interface{}{"development": true} - s.assertUploadTools(c, vers, true, extraCfg, "") + s.assertUploadTools(c, vers, false, extraCfg, "") } func (s *bootstrapSuite) TestUploadToolsForceVersionAllowsAgentVersionSet(c *gc.C) { @@ -390,11 +413,6 @@ s.assertUploadTools(c, vers, true, extraCfg, "") } -func (s *bootstrapSuite) TestUploadToolsReleaseVersionDisallowed(c *gc.C) { - vers := version.MustParseBinary("1.18.0-trusty-arm64") - s.assertUploadTools(c, vers, false, nil, "Juju cannot bootstrap because no tools are available for your environment.*") -} - type bootstrapEnviron struct { name string cfg *config.Config diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/environs/bootstrap/synctools.go juju-core-1.18.0/src/launchpad.net/juju-core/environs/bootstrap/synctools.go --- juju-core-1.17.7/src/launchpad.net/juju-core/environs/bootstrap/synctools.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/environs/bootstrap/synctools.go 2014-04-04 16:57:31.000000000 +0000 @@ -29,8 +29,7 @@ // UploadTools uploads tools for the specified series and any other relevant series to // the environment storage, after which it sets the agent-version. If forceVersion is true, -// we allow uploading release tools versions and allow uploading even when the agent-version is -// already set in the environment. +// we allow uploading even when the agent-version is already set in the environment. func UploadTools(ctx environs.BootstrapContext, env environs.Environ, toolsArch *string, forceVersion bool, bootstrapSeries ...string) error { logger.Infof("checking that upload is possible") // Check the series are valid. @@ -100,8 +99,10 @@ unique := set.NewStrings(series...) if unique.IsEmpty() { unique.Add(version.Current.Series) - unique.Add(config.DefaultSeries) - unique.Add(cfg.DefaultSeries()) + unique.Add(config.LatestLtsSeries()) + if series, ok := cfg.DefaultSeries(); ok { + unique.Add(series) + } } return unique.Values() } @@ -110,9 +111,8 @@ // not be allowed. func validateUploadAllowed(env environs.Environ, toolsArch *string, forceVersion bool) error { if !forceVersion { - // First, check that there isn't already an agent version specified, and that we - // are running a development version. - if _, hasAgentVersion := env.Config().AgentVersion(); hasAgentVersion || !version.Current.IsDev() { + // First, check that there isn't already an agent version specified. + if _, hasAgentVersion := env.Config().AgentVersion(); hasAgentVersion { return fmt.Errorf(noToolsNoUploadMessage) } } @@ -172,10 +172,18 @@ return nil, err } + // Only automatically upload tools for dev versions. + if !version.Current.IsDev() { + return nil, fmt.Errorf("cannot upload bootstrap tools: %v", noToolsNoUploadMessage) + } + // No tools available so our only hope is to build locally and upload. logger.Warningf("no prepackaged tools available") uploadSeries := SeriesToUpload(cfg, nil) - if err := UploadTools(ctx, env, toolsArch, false, append(uploadSeries, series)...); err != nil { + if series != "" { + uploadSeries = append(uploadSeries, series) + } + if err := UploadTools(ctx, env, toolsArch, false, uploadSeries...); err != nil { logger.Errorf("%s", noToolsMessage) return nil, fmt.Errorf("cannot upload bootstrap tools: %v", err) } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/environs/cloudinit/cloudinit.go juju-core-1.18.0/src/launchpad.net/juju-core/environs/cloudinit/cloudinit.go --- juju-core-1.17.7/src/launchpad.net/juju-core/environs/cloudinit/cloudinit.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/environs/cloudinit/cloudinit.go 2014-04-04 16:57:31.000000000 +0000 @@ -221,6 +221,7 @@ // juju requires git for managing charm directories. c.AddPackage("git") + c.AddPackage("curl") c.AddPackage("cpu-checker") c.AddPackage("bridge-utils") c.AddPackage("rsyslog-gnutls") diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/environs/config/config.go juju-core-1.18.0/src/launchpad.net/juju-core/environs/config/config.go --- juju-core-1.17.7/src/launchpad.net/juju-core/environs/config/config.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/environs/config/config.go 2014-04-04 16:57:31.000000000 +0000 @@ -7,6 +7,7 @@ "fmt" "io/ioutil" "os" + "os/exec" "path/filepath" "regexp" "strings" @@ -34,9 +35,6 @@ // port opened. FwGlobal = "global" - // DefaultSeries returns the most recent Ubuntu LTS release name. - DefaultSeries string = "precise" - // DefaultStatePort is the default port the state server is listening on. DefaultStatePort int = 37017 @@ -59,8 +57,53 @@ // refreshing the addresses, in seconds. Not too frequent, as we // refresh addresses from the provider each time. DefaultBootstrapSSHAddressesDelay int = 10 + + // fallbackLtsSeries is the latest LTS series we'll use, if we fail to + // obtain this information from the system. + fallbackLtsSeries string = "precise" ) +var latestLtsSeries string + +type HasDefaultSeries interface { + DefaultSeries() (string, bool) +} + +// PreferredSeries returns the preferred series to use when a charm does not +// explicitly specify a series. +func PreferredSeries(cfg HasDefaultSeries) string { + if series, ok := cfg.DefaultSeries(); ok { + return series + } + return LatestLtsSeries() +} + +func LatestLtsSeries() string { + if latestLtsSeries == "" { + series, err := distroLtsSeries() + if err != nil { + latestLtsSeries = fallbackLtsSeries + } else { + latestLtsSeries = series + } + } + return latestLtsSeries +} + +// distroLtsSeries returns the latest LTS series, if this information is +// available on this system. +func distroLtsSeries() (string, error) { + out, err := exec.Command("distro-info", "--lts").Output() + if err != nil { + return "", err + } + series := strings.TrimSpace(string(out)) + if !charm.IsValidSeries(series) { + return "", fmt.Errorf("not a valid LTS series: %q", series) + } + return series, nil +} + // Config holds an immutable environment configuration. type Config struct { // defined holds the attributes that are defined for Config. @@ -162,7 +205,6 @@ // For backward compatibility purposes, we treat as unset string // valued attributes that are set to the empty string, and fill // out their defaults accordingly. - c.fillInStringDefault("default-series") c.fillInStringDefault("firewall-mode") // Load authorized-keys-path into authorized-keys if necessary. @@ -407,9 +449,17 @@ return c.mustString("name") } -// DefaultSeries returns the default Ubuntu series for the environment. -func (c *Config) DefaultSeries() string { - return c.mustString("default-series") +// DefaultSeries returns the configured default Ubuntu series for the environment, +// and whether the default series was explicitly configured on the environment. +func (c *Config) DefaultSeries() (string, bool) { + if s, ok := c.defined["default-series"]; ok { + if series, ok := s.(string); ok && series != "" { + return series, true + } else if !ok { + logger.Warningf("invalid default-series: %q", s) + } + } + return "", false } // StatePort returns the state server port for the environment. @@ -763,6 +813,8 @@ "image-metadata-url": "", // TODO(rog) omit "tools-metadata-url": "", // TODO(rog) omit + "default-series": "", + // For backward compatibility only - default ports were // not filled out in previous versions of the configuration. "state-port": DefaultStatePort, @@ -786,7 +838,6 @@ // UseDefaults. func allDefaults() schema.Defaults { d := schema.Defaults{ - "default-series": DefaultSeries, "firewall-mode": FwInstance, "development": false, "ssl-hostname-verification": true, diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/environs/config/config_test.go juju-core-1.18.0/src/launchpad.net/juju-core/environs/config/config_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/environs/config/config_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/environs/config/config_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -652,7 +652,6 @@ authTokenConfigTest("token=value, =z", false), authTokenConfigTest("token=value =z", false), authTokenConfigTest("\t", false), - missingAttributeNoDefault("default-series"), missingAttributeNoDefault("firewall-mode"), missingAttributeNoDefault("development"), missingAttributeNoDefault("ssl-hostname-verification"), @@ -861,11 +860,14 @@ testmode, _ := test.attrs["test-mode"].(bool) c.Assert(cfg.TestMode(), gc.Equals, testmode) - if series, _ := test.attrs["default-series"].(string); series != "" { - c.Assert(cfg.DefaultSeries(), gc.Equals, series) + series, _ := test.attrs["default-series"].(string) + if defaultSeries, ok := cfg.DefaultSeries(); ok { + c.Assert(defaultSeries, gc.Equals, series) } else { - c.Assert(cfg.DefaultSeries(), gc.Equals, config.DefaultSeries) + c.Assert(series, gc.Equals, "") + c.Assert(defaultSeries, gc.Equals, "") } + if m, _ := test.attrs["firewall-mode"].(string); m != "" { c.Assert(cfg.FirewallMode(), gc.Equals, m) } @@ -1010,7 +1012,7 @@ "bootstrap-timeout": 3600, "bootstrap-retry-delay": 30, "bootstrap-addresses-delay": 10, - "default-series": "precise", + "default-series": testing.FakeDefaultSeries, "charm-store-auth": "token=auth", "test-mode": false, } @@ -1019,7 +1021,6 @@ // These attributes are added if not set. attrs["development"] = false - attrs["default-series"] = config.DefaultSeries attrs["logging-config"] = "=WARNING;unit=DEBUG" attrs["ca-private-key"] = "" attrs["image-metadata-url"] = "" diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/environs/instances/instancetype_test.go juju-core-1.18.0/src/launchpad.net/juju-core/environs/instances/instancetype_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/environs/instances/instancetype_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/environs/instances/instancetype_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -25,7 +25,7 @@ var instanceTypes = []InstanceType{ { Name: "m1.small", - Arches: []string{"amd64", "arm"}, + Arches: []string{"amd64", "armhf"}, CpuCores: 1, CpuPower: CpuPower(100), Mem: 1740, @@ -33,7 +33,7 @@ RootDisk: 8192, }, { Name: "m1.medium", - Arches: []string{"amd64", "arm"}, + Arches: []string{"amd64", "armhf"}, CpuCores: 1, CpuPower: CpuPower(200), Mem: 3840, @@ -58,7 +58,7 @@ }, { Name: "t1.micro", - Arches: []string{"amd64", "arm"}, + Arches: []string{"amd64", "armhf"}, CpuCores: 1, CpuPower: CpuPower(20), Mem: 613, @@ -67,7 +67,7 @@ }, { Name: "c1.medium", - Arches: []string{"amd64", "arm"}, + Arches: []string{"amd64", "armhf"}, CpuCores: 2, CpuPower: CpuPower(500), Mem: 1740, @@ -135,9 +135,9 @@ }, }, { about: "arches filtered by constraint", - cons: "cpu-power=100 arch=arm", + cons: "cpu-power=100 arch=armhf", expectedItypes: []string{"m1.small", "m1.medium", "c1.medium"}, - arches: []string{"arm"}, + arches: []string{"armhf"}, }, { about: "enough memory for mongodb if mem not specified", @@ -165,7 +165,7 @@ about: "mem specified and match found", cons: "mem=4G arch=amd64", itypesToUse: []InstanceType{ - {Id: "4", Name: "it-4", Arches: []string{"arm"}, Mem: 8096}, + {Id: "4", Name: "it-4", Arches: []string{"armhf"}, Mem: 8096}, {Id: "3", Name: "it-3", Arches: []string{"amd64"}, Mem: 4096}, {Id: "2", Name: "it-2", Arches: []string{"amd64"}, Mem: 2048}, {Id: "1", Name: "it-1", Arches: []string{"amd64"}, Mem: 512}, @@ -243,22 +243,22 @@ itype string arches []string }{ - {"", "m1.small", []string{"amd64", "arm"}}, + {"", "m1.small", []string{"amd64", "armhf"}}, {"", "m1.large", []string{"amd64"}}, - {"cpu-power=100", "m1.small", []string{"amd64", "arm"}}, + {"cpu-power=100", "m1.small", []string{"amd64", "armhf"}}, {"arch=amd64", "m1.small", []string{"amd64"}}, {"cpu-cores=3", "m1.xlarge", []string{"amd64"}}, - {"cpu-power=", "t1.micro", []string{"amd64", "arm"}}, - {"cpu-power=500", "c1.medium", []string{"amd64", "arm"}}, + {"cpu-power=", "t1.micro", []string{"amd64", "armhf"}}, + {"cpu-power=500", "c1.medium", []string{"amd64", "armhf"}}, {"cpu-power=2000", "c1.xlarge", []string{"amd64"}}, {"cpu-power=2001", "cc1.4xlarge", []string{"amd64"}}, - {"mem=2G", "m1.medium", []string{"amd64", "arm"}}, + {"mem=2G", "m1.medium", []string{"amd64", "armhf"}}, {"arch=i386", "m1.small", nil}, {"cpu-power=100", "t1.micro", nil}, {"cpu-power=9001", "cc2.8xlarge", nil}, {"mem=1G", "t1.micro", nil}, - {"arch=arm", "c1.xlarge", nil}, + {"arch=armhf", "c1.xlarge", nil}, } func (s *instanceTypeSuite) TestMatch(c *gc.C) { diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/environs/interface.go juju-core-1.18.0/src/launchpad.net/juju-core/environs/interface.go --- juju-core-1.17.7/src/launchpad.net/juju-core/environs/interface.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/environs/interface.go 2014-04-04 16:57:31.000000000 +0000 @@ -57,12 +57,6 @@ // which are considered sensitive. All of the values of these secret // attributes need to be strings. SecretAttrs(cfg *config.Config) (map[string]string, error) - - // PublicAddress returns this machine's public host name. - PublicAddress() (string, error) - - // PrivateAddress returns this machine's private host name. - PrivateAddress() (string, error) } // EnvironStorage implements storage access for an environment diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/environs/jujutest/livetests.go juju-core-1.18.0/src/launchpad.net/juju-core/environs/jujutest/livetests.go --- juju-core-1.17.7/src/launchpad.net/juju-core/environs/jujutest/livetests.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/environs/jujutest/livetests.go 2014-04-04 16:57:31.000000000 +0000 @@ -133,7 +133,7 @@ // we could connect to (actual live tests, rather than local-only) cons := constraints.MustParse("mem=2G") if t.CanOpenState { - _, err := sync.Upload(t.Env.Storage(), nil, config.DefaultSeries) + _, err := sync.Upload(t.Env.Storage(), nil, coretesting.FakeDefaultSeries) c.Assert(err, gc.IsNil) } envtesting.UploadFakeTools(c, t.Env.Storage()) @@ -442,7 +442,7 @@ // If the series has not been specified, we expect the most recent Ubuntu LTS release to be used. expectedVersion := version.Current - expectedVersion.Series = config.DefaultSeries + expectedVersion.Series = config.LatestLtsSeries() mtools0 := waitAgentTools(c, mw0, expectedVersion) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/environs/manual/init_test.go juju-core-1.18.0/src/launchpad.net/juju-core/environs/manual/init_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/environs/manual/init_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/environs/manual/init_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -48,7 +48,7 @@ defer installFakeSSH(c, manual.DetectionScript, []string{scriptResponse, "non-empty-stderr"}, 0)() hc, _, err = manual.DetectSeriesAndHardwareCharacteristics("hostname") c.Assert(err, gc.IsNil) - c.Assert(hc.String(), gc.Equals, "arch=arm cpu-cores=1 mem=4M") + c.Assert(hc.String(), gc.Equals, "arch=armhf cpu-cores=1 mem=4M") } func (s *initialisationSuite) TestDetectHardwareCharacteristics(c *gc.C) { @@ -59,7 +59,7 @@ }{{ "Single CPU socket, single core, no hyper-threading", []string{"edgy", "armv4", "MemTotal: 4096 kB", "processor: 0"}, - "arch=arm cpu-cores=1 mem=4M", + "arch=armhf cpu-cores=1 mem=4M", }, { "Single CPU socket, single core, hyper-threading", []string{ @@ -71,7 +71,7 @@ "physical id: 0", "cpu cores: 1", }, - "arch=arm cpu-cores=1 mem=4M", + "arch=armhf cpu-cores=1 mem=4M", }, { "Single CPU socket, dual-core, no hyper-threading", []string{ @@ -83,7 +83,7 @@ "physical id: 0", "cpu cores: 2", }, - "arch=arm cpu-cores=2 mem=4M", + "arch=armhf cpu-cores=2 mem=4M", }, { "Dual CPU socket, each single-core, hyper-threading", []string{ @@ -101,7 +101,7 @@ "physical id: 1", "cpu cores: 1", }, - "arch=arm cpu-cores=2 mem=4M", + "arch=armhf cpu-cores=2 mem=4M", }} for i, test := range tests { c.Logf("test %d: %s", i, test.summary) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/environs/testing/tools.go juju-core-1.18.0/src/launchpad.net/juju-core/environs/testing/tools.go --- juju-core-1.17.7/src/launchpad.net/juju-core/environs/testing/tools.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/environs/testing/tools.go 2014-04-04 16:53:35.000000000 +0000 @@ -13,7 +13,6 @@ agenttools "launchpad.net/juju-core/agent/tools" "launchpad.net/juju-core/environs" - "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/environs/simplestreams" "launchpad.net/juju-core/environs/storage" envtools "launchpad.net/juju-core/environs/tools" @@ -163,8 +162,9 @@ func uploadFakeTools(stor storage.Storage) error { versions := []version.Binary{version.Current} toolsVersion := version.Current - if toolsVersion.Series != config.DefaultSeries { - toolsVersion.Series = config.DefaultSeries + latestLts := coretesting.FakeDefaultSeries + if toolsVersion.Series != latestLts { + toolsVersion.Series = latestLts versions = append(versions, toolsVersion) } if _, err := UploadFakeToolsVersions(stor, versions...); err != nil { @@ -175,9 +175,9 @@ // UploadFakeTools puts fake tools into the supplied storage with a binary // version matching version.Current; if version.Current's series is different -// to config.DefaultSeries, matching fake tools will be uploaded for that series. -// This is useful for tests that are kinda casual about specifying their -// environment. +// to coretesting.FakeDefaultSeries, matching fake tools will be uploaded for that +// series. This is useful for tests that are kinda casual about specifying +// their environment. func UploadFakeTools(c *gc.C, stor storage.Storage) { c.Assert(uploadFakeTools(stor), gc.IsNil) } @@ -196,8 +196,9 @@ name := envtools.StorageName(toolsVersion) err := stor.Remove(name) c.Check(err, gc.IsNil) - if version.Current.Series != config.DefaultSeries { - toolsVersion.Series = config.DefaultSeries + defaultSeries := coretesting.FakeDefaultSeries + if version.Current.Series != defaultSeries { + toolsVersion.Series = defaultSeries name := envtools.StorageName(toolsVersion) err := stor.Remove(name) c.Check(err, gc.IsNil) @@ -380,7 +381,7 @@ Available: VAll, CliVersion: V100p64, DefaultSeries: "precise", - Arch: "arm", + Arch: "armhf", Err: noToolsMessage, }, { Info: "released cli: specific bad major 1", diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/instance/instance_test.go juju-core-1.18.0/src/launchpad.net/juju-core/instance/instance_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/instance/instance_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/instance/instance_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -49,8 +49,8 @@ summary: "set arch i386", args: []string{"arch=i386"}, }, { - summary: "set arch arm", - args: []string{"arch=arm"}, + summary: "set arch armhf", + args: []string{"arch=armhf"}, }, { summary: "set nonsense arch 1", args: []string{"arch=cheese"}, @@ -65,7 +65,7 @@ err: `bad "arch" characteristic: already set`, }, { summary: "double set arch separately", - args: []string{"arch=arm", "arch="}, + args: []string{"arch=armhf", "arch="}, err: `bad "arch" characteristic: already set`, }, @@ -223,7 +223,7 @@ args: []string{" root-disk=4G mem=2T arch=i386 cpu-cores=4096 cpu-power=9001"}, }, { summary: "kitchen sink separately", - args: []string{"root-disk=4G", "mem=2T", "cpu-cores=4096", "cpu-power=9001", "arch=arm"}, + args: []string{"root-disk=4G", "mem=2T", "cpu-cores=4096", "cpu-power=9001", "arch=armhf"}, }, } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/juju/apiconn_test.go juju-core-1.18.0/src/launchpad.net/juju-core/juju/apiconn_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/juju/apiconn_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/juju/apiconn_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -160,7 +160,7 @@ "name": "myenv", "state-server": true, "authorized-keys": "i-am-a-key", - "default-series": config.DefaultSeries, + "default-series": config.LatestLtsSeries(), "firewall-mode": config.FwInstance, "development": false, "ssl-hostname-verification": true, diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/juju/arch/arch.go juju-core-1.18.0/src/launchpad.net/juju-core/juju/arch/arch.go --- juju-core-1.17.7/src/launchpad.net/juju-core/juju/arch/arch.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/juju/arch/arch.go 2014-04-04 16:53:35.000000000 +0000 @@ -13,7 +13,7 @@ const ( AMD64 = "amd64" I386 = "i386" - ARM = "arm" + ARM = "armhf" ARM64 = "arm64" PPC64 = "ppc64" ) @@ -35,7 +35,7 @@ }{ {regexp.MustCompile("amd64|x86_64"), AMD64}, {regexp.MustCompile("i?[3-9]86"), I386}, - {regexp.MustCompile("armv.*"), ARM}, + {regexp.MustCompile("arm|armv.*"), ARM}, {regexp.MustCompile("aarch64"), ARM64}, {regexp.MustCompile("ppc64el|ppc64le"), PPC64}, } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/juju/arch/arch_test.go juju-core-1.18.0/src/launchpad.net/juju-core/juju/arch/arch_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/juju/arch/arch_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/juju/arch/arch_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -33,8 +33,9 @@ {"386", "i386"}, {"i386", "i386"}, {"i486", "i386"}, - {"armv", "arm"}, - {"armv7", "arm"}, + {"arm", "armhf"}, + {"armv", "armhf"}, + {"armv7", "armhf"}, {"aarch64", "arm64"}, {"ppc64el", "ppc64"}, {"ppc64le", "ppc64"}, diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/juju/testing/conn.go juju-core-1.18.0/src/launchpad.net/juju-core/juju/testing/conn.go --- juju-core-1.17.7/src/launchpad.net/juju-core/juju/testing/conn.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/juju/testing/conn.go 2014-04-04 16:57:31.000000000 +0000 @@ -17,6 +17,7 @@ "launchpad.net/juju-core/constraints" "launchpad.net/juju-core/environs" "launchpad.net/juju-core/environs/bootstrap" + "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/environs/configstore" envtesting "launchpad.net/juju-core/environs/testing" "launchpad.net/juju-core/juju" @@ -164,6 +165,14 @@ return s.openAPIAs(c, machine.Tag(), password, "fake_nonce"), machine } +func PreferredDefaultVersions(conf *config.Config, template version.Binary) []version.Binary { + prefVersion := template + prefVersion.Series = config.PreferredSeries(conf) + defaultVersion := template + defaultVersion.Series = testing.FakeDefaultSeries + return []version.Binary{prefVersion, defaultVersion} +} + func (s *JujuConnSuite) setUpConn(c *gc.C) { if s.RootDir != "" { panic("JujuConnSuite.setUpConn without teardown") @@ -203,7 +212,11 @@ c.Assert(environ.Name(), gc.Equals, "dummyenv") s.PatchValue(&dummy.DataDir, s.DataDir()) - envtesting.MustUploadFakeTools(environ.Storage()) + versions := PreferredDefaultVersions(environ.Config(), version.Current) + versions = append(versions, version.Current) + + // Upload tools for both preferred and fake default series + envtesting.MustUploadFakeToolsVersions(environ.Storage(), versions...) c.Assert(bootstrap.Bootstrap(ctx, environ, constraints.Value{}), gc.IsNil) s.BackingState = environ.(GetStater).GetStateInAPIServer() @@ -285,7 +298,7 @@ ch := testing.Charms.Dir(name) ident := fmt.Sprintf("%s-%d", ch.Meta().Name, ch.Revision()) curl := charm.MustParseURL("local:quantal/" + ident) - repo, err := charm.InferRepository(curl, testing.Charms.Path()) + repo, err := charm.InferRepository(curl.Reference, testing.Charms.Path()) c.Assert(err, gc.IsNil) sch, err := s.Conn.PutCharm(curl, repo, false) c.Assert(err, gc.IsNil) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/juju/testing/instance.go juju-core-1.18.0/src/launchpad.net/juju-core/juju/testing/instance.go --- juju-core-1.17.7/src/launchpad.net/juju-core/juju/testing/instance.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/juju/testing/instance.go 2014-04-04 16:57:31.000000000 +0000 @@ -10,6 +10,7 @@ "launchpad.net/juju-core/constraints" "launchpad.net/juju-core/environs" + "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/environs/tools" "launchpad.net/juju-core/instance" "launchpad.net/juju-core/names" @@ -110,7 +111,7 @@ ) ( instance.Instance, *instance.HardwareCharacteristics, error, ) { - series := env.Config().DefaultSeries() + series := config.PreferredSeries(env.Config()) agentVersion, ok := env.Config().AgentVersion() if !ok { return nil, nil, fmt.Errorf("missing agent version in environment config") diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/all/all.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/all/all.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/all/all.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/all/all.go 2014-04-04 16:53:35.000000000 +0000 @@ -7,7 +7,7 @@ import ( _ "launchpad.net/juju-core/provider/azure" _ "launchpad.net/juju-core/provider/ec2" - //_ "launchpad.net/juju-core/provider/joyent" + _ "launchpad.net/juju-core/provider/joyent" _ "launchpad.net/juju-core/provider/local" _ "launchpad.net/juju-core/provider/maas" _ "launchpad.net/juju-core/provider/manual" diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/azure/environprovider.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/azure/environprovider.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/azure/environprovider.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/azure/environprovider.go 2014-04-04 16:57:31.000000000 +0000 @@ -4,10 +4,6 @@ package azure import ( - "encoding/xml" - "fmt" - "io/ioutil" - "github.com/juju/loggo" "launchpad.net/juju-core/environs" @@ -46,104 +42,3 @@ // TODO prepare environment as necessary return prov.Open(cfg) } - -// PublicAddress is specified in the EnvironProvider interface. -func (prov azureEnvironProvider) PublicAddress() (string, error) { - config, err := parseWALAConfig() - if err != nil { - logger.Errorf("error parsing Windows Azure Linux Agent config file (%q): %v", _WALAConfigPath, err) - return "", err - } - return config.getDeploymentFQDN(), nil -} - -// PrivateAddress is specified in the EnvironProvider interface. -func (prov azureEnvironProvider) PrivateAddress() (string, error) { - config, err := parseWALAConfig() - if err != nil { - logger.Errorf("error parsing Windows Azure Linux Agent config file (%q): %v", _WALAConfigPath, err) - return "", err - } - return config.getInternalIP(), nil -} - -// The XML Windows Azure Linux Agent (WALA) is the agent which runs on all -// the Linux Azure VMs. The hostname of the VM is the service name and the -// juju instanceId is (by design), the deployment's name. -// -// See https://github.com/windows-azure/walinuxagent for more details. -// -// Here is an example content of such a config file: -// -// -// -// -// -// [...] -// -// -// [...] -// -// -// -// - -// Structures used to parse the XML Windows Azure Linux Agent (WALA) -// configuration file. - -type WALASharedConfig struct { - XMLName xml.Name `xml:"SharedConfig"` - Deployment WALADeployment `xml:"Deployment"` - Instances []WALAInstance `xml:"Instances>Instance"` -} - -// getDeploymentName returns the deployment name referenced by the -// configuration. -// Confusingly, this is stored in the 'name' attribute of the 'Service' -// element. -func (config *WALASharedConfig) getDeploymentName() string { - return config.Deployment.Service.Name -} - -// getDeploymentFQDN returns the FQDN of this deployment. -// The hostname is taken from the 'name' attribute of the Service element -// embedded in the Deployment element. The domain name is Azure's fixed -// domain name: 'cloudapp.net'. -func (config *WALASharedConfig) getDeploymentFQDN() string { - return fmt.Sprintf("%s.%s", config.getDeploymentName(), AZURE_DOMAIN_NAME) -} - -// getInternalIP returns the internal IP for this deployment. -// The internalIP is the internal IP of the only instance in this deployment. -func (config *WALASharedConfig) getInternalIP() string { - return config.Instances[0].Address -} - -type WALADeployment struct { - Name string `xml:"name,attr"` - Service WALADeploymentService `xml:"Service"` -} - -type WALADeploymentService struct { - Name string `xml:"name,attr"` -} - -type WALAInstance struct { - Address string `xml:"address,attr"` -} - -// Path to the WALA configuration file. -var _WALAConfigPath = "/var/lib/waagent/SharedConfig.xml" - -func parseWALAConfig() (*WALASharedConfig, error) { - data, err := ioutil.ReadFile(_WALAConfigPath) - if err != nil { - return nil, err - } - config := &WALASharedConfig{} - err = xml.Unmarshal(data, config) - if err != nil { - return nil, err - } - return config, nil -} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/azure/environprovider_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/azure/environprovider_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/azure/environprovider_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/azure/environprovider_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -4,9 +4,6 @@ package azure import ( - "fmt" - "io/ioutil" - gc "launchpad.net/gocheck" "launchpad.net/juju-core/environs/config" @@ -46,112 +43,3 @@ c.Check(env, gc.Equals, nil) c.Check(err, gc.ErrorMatches, ".*environment has no location; you need to set one.*") } - -// writeWALASharedConfig creates a temporary file with a valid WALinux config -// built using the given parameters. The file will be cleaned up at the end -// of the test calling this method. -func writeWALASharedConfig(c *gc.C, deploymentId string, deploymentName string, internalAddress string) string { - configTemplateXML := ` - - - - - - - - - - ` - config := fmt.Sprintf(configTemplateXML, deploymentId, deploymentName, deploymentId, internalAddress) - file, err := ioutil.TempFile(c.MkDir(), "") - c.Assert(err, gc.IsNil) - filename := file.Name() - err = ioutil.WriteFile(filename, []byte(config), 0644) - c.Assert(err, gc.IsNil) - return filename -} - -// overrideWALASharedConfig: -// - creates a temporary file with a valid WALinux config built using the -// given parameters. The file will be cleaned up at the end of the test -// calling this method. -// - monkey patches the value of '_WALAConfigPath' (the path to the WALA -// configuration file) so that it contains the path to the temporary file. -// overrideWALASharedConfig returns a cleanup method that the caller *must* -// call in order to restore the original value of '_WALAConfigPath' -func overrideWALASharedConfig(c *gc.C, deploymentId, deploymentName, internalAddress string) func() { - filename := writeWALASharedConfig(c, deploymentId, deploymentName, - internalAddress) - oldConfigPath := _WALAConfigPath - _WALAConfigPath = filename - // Return cleanup method to restore the original value of - // '_WALAConfigPath'. - return func() { - _WALAConfigPath = oldConfigPath - } -} - -func (*environProviderSuite) TestParseWALASharedConfig(c *gc.C) { - deploymentId := "b6de4c4c7d4a49c39270e0c57481fd9b" - deploymentName := "gwaclmachineex95rsek" - internalAddress := "10.76.200.59" - - cleanup := overrideWALASharedConfig(c, deploymentId, deploymentName, internalAddress) - defer cleanup() - - config, err := parseWALAConfig() - c.Assert(err, gc.IsNil) - c.Check(config.Deployment.Name, gc.Equals, deploymentId) - c.Check(config.Deployment.Service.Name, gc.Equals, deploymentName) - c.Check(config.Instances[0].Address, gc.Equals, internalAddress) -} - -func (*environProviderSuite) TestConfigGetDeploymentFQDN(c *gc.C) { - deploymentId := "b6de4c4c7d4a49c39270e0c57481fd9b" - serviceName := "gwaclr12slechtstschrijvende5" - config := WALASharedConfig{ - Deployment: WALADeployment{ - Name: deploymentId, - Service: WALADeploymentService{Name: serviceName}, - }, - } - - c.Check(config.getDeploymentFQDN(), gc.Equals, serviceName+".cloudapp.net") -} - -func (*environProviderSuite) TestConfigGetDeploymentHostname(c *gc.C) { - deploymentName := "gwaclmachineex95rsek" - config := WALASharedConfig{Deployment: WALADeployment{Name: "id", Service: WALADeploymentService{Name: deploymentName}}} - - c.Check(config.getDeploymentName(), gc.Equals, deploymentName) -} - -func (*environProviderSuite) TestConfigGetInternalIP(c *gc.C) { - internalAddress := "10.76.200.59" - config := WALASharedConfig{Instances: []WALAInstance{{Address: internalAddress}}} - - c.Check(config.getInternalIP(), gc.Equals, internalAddress) -} - -func (*environProviderSuite) TestPublicAddress(c *gc.C) { - deploymentName := "b6de4c4c7d4a49c39270e0c57481fd9b" - cleanup := overrideWALASharedConfig(c, "deploymentid", deploymentName, "10.76.200.59") - defer cleanup() - - expectedAddress := deploymentName + ".cloudapp.net" - prov := azureEnvironProvider{} - pubAddress, err := prov.PublicAddress() - c.Assert(err, gc.IsNil) - c.Check(pubAddress, gc.Equals, expectedAddress) -} - -func (*environProviderSuite) TestPrivateAddress(c *gc.C) { - internalAddress := "10.76.200.59" - cleanup := overrideWALASharedConfig(c, "deploy-id", "name", internalAddress) - defer cleanup() - - prov := azureEnvironProvider{} - privAddress, err := prov.PrivateAddress() - c.Assert(err, gc.IsNil) - c.Check(privAddress, gc.Equals, internalAddress) -} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/azure/environ_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/azure/environ_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/azure/environ_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/azure/environ_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -411,7 +411,7 @@ err = env.SetConfig(cfg) c.Assert(err, gc.IsNil) - c.Check(env.ecfg.Config.DefaultSeries(), gc.Equals, "feisty") + c.Check(config.PreferredSeries(env.ecfg.Config), gc.Equals, "feisty") } func (*environSuite) TestSetConfigLocksEnviron(c *gc.C) { diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/common/bootstrap.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/common/bootstrap.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/common/bootstrap.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/common/bootstrap.go 2014-04-04 16:57:31.000000000 +0000 @@ -42,7 +42,7 @@ defer func() { handleBootstrapError(err, ctx, inst, env) }() // First thing, ensure we have tools otherwise there's no point. - selectedTools, err := EnsureBootstrapTools(ctx, env, env.Config().DefaultSeries(), cons.Arch) + selectedTools, err := EnsureBootstrapTools(ctx, env, config.PreferredSeries(env.Config()), cons.Arch) if err != nil { return err } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/dummy/environs.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/dummy/environs.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/dummy/environs.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/dummy/environs.go 2014-04-04 16:57:31.000000000 +0000 @@ -483,14 +483,6 @@ }, nil } -func (*environProvider) PublicAddress() (string, error) { - return "public.dummy.address.example.com", nil -} - -func (*environProvider) PrivateAddress() (string, error) { - return "private.dummy.address.example.com", nil -} - func (*environProvider) BoilerplateConfig() string { return ` # Fake configuration for dummy provider. @@ -543,7 +535,7 @@ } func (e *environ) Bootstrap(ctx environs.BootstrapContext, cons constraints.Value) error { - selectedTools, err := common.EnsureBootstrapTools(ctx, e, e.Config().DefaultSeries(), cons.Arch) + selectedTools, err := common.EnsureBootstrapTools(ctx, e, config.PreferredSeries(e.Config()), cons.Arch) if err != nil { return err } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/ec2/ec2.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/ec2/ec2.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/ec2/ec2.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/ec2/ec2.go 2014-04-04 16:57:31.000000000 +0000 @@ -5,9 +5,6 @@ import ( "fmt" - "io/ioutil" - "net/http" - "strings" "sync" "time" @@ -264,14 +261,6 @@ return m, nil } -func (environProvider) PublicAddress() (string, error) { - return fetchMetadata("public-hostname") -} - -func (environProvider) PrivateAddress() (string, error) { - return fetchMetadata("local-hostname") -} - func (e *environ) Config() *config.Config { return e.ecfg().Config } @@ -368,7 +357,7 @@ return nil, err } return &simplestreams.MetadataLookupParams{ - Series: e.ecfg().DefaultSeries(), + Series: config.PreferredSeries(e.ecfg()), Region: cloudSpec.Region, Endpoint: cloudSpec.Endpoint, Architectures: arch.AllSupportedArches, @@ -1057,37 +1046,6 @@ return ec2err.Code } -// metadataHost holds the address of the instance metadata service. -// It is a variable so that tests can change it to refer to a local -// server when needed. -var metadataHost = "http://169.254.169.254" - -// fetchMetadata fetches a single atom of data from the ec2 instance metadata service. -// http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html -func fetchMetadata(name string) (value string, err error) { - uri := fmt.Sprintf("%s/2011-01-01/meta-data/%s", metadataHost, name) - defer utils.ErrorContextf(&err, "cannot get %q", uri) - for a := shortAttempt.Start(); a.Next(); { - var resp *http.Response - resp, err = http.Get(uri) - if err != nil { - continue - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - err = fmt.Errorf("bad http response %v", resp.Status) - continue - } - var data []byte - data, err = ioutil.ReadAll(resp.Body) - if err != nil { - continue - } - return strings.TrimSpace(string(data)), nil - } - return -} - // GetImageSources returns a list of sources which are used to search for simplestreams image metadata. func (e *environ) GetImageSources() ([]simplestreams.DataSource, error) { // Add the simplestreams source off the control bucket. diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/ec2/export_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/ec2/export_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/ec2/export_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/ec2/export_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -98,18 +98,6 @@ } } -var origMetadataHost = metadataHost - -func UseTestMetadata(files map[string]string) { - if files != nil { - testRoundTripper.Sub = jujutest.NewCannedRoundTripper(files, nil) - metadataHost = "test:" - } else { - testRoundTripper.Sub = nil - metadataHost = origMetadataHost - } -} - var ( ShortAttempt = &shortAttempt StorageAttempt = &storageAttempt diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/ec2/live_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/ec2/live_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/ec2/live_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/ec2/live_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -101,7 +101,7 @@ t.LiveTests.SetUpTest(c) t.PatchValue(&version.Current, version.Binary{ Number: version.Current.Number, - Series: config.DefaultSeries, + Series: coretesting.FakeDefaultSeries, Arch: arch.AMD64, }) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/ec2/local_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/ec2/local_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/ec2/local_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/ec2/local_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -45,26 +45,6 @@ var _ = gc.Suite(&ProviderSuite{}) -func (s *ProviderSuite) TestMetadata(c *gc.C) { - metadataContent := map[string]string{ - "/2011-01-01/meta-data/public-hostname": "public.dummy.address.invalid", - "/2011-01-01/meta-data/local-hostname": "private.dummy.address.invalid", - } - ec2.UseTestMetadata(metadataContent) - defer ec2.UseTestMetadata(nil) - - p, err := environs.Provider("ec2") - c.Assert(err, gc.IsNil) - - addr, err := p.PublicAddress() - c.Assert(err, gc.IsNil) - c.Assert(addr, gc.Equals, "public.dummy.address.invalid") - - addr, err = p.PrivateAddress() - c.Assert(err, gc.IsNil) - c.Assert(addr, gc.Equals, "private.dummy.address.invalid") -} - func (t *ProviderSuite) assertGetImageMetadataSources(c *gc.C, stream, officialSourcePath string) { // Make an env configured with the stream. envAttrs := localConfigAttrs @@ -225,7 +205,7 @@ t.Tests.SetUpTest(c) t.PatchValue(&version.Current, version.Binary{ Number: version.Current.Number, - Series: config.DefaultSeries, + Series: coretesting.FakeDefaultSeries, Arch: arch.AMD64, }) } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/config.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/config.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/config.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/config.go 2014-04-04 16:53:35.000000000 +0000 @@ -5,7 +5,10 @@ import ( "fmt" + "io/ioutil" + "net/url" "os" + "strings" "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/schema" @@ -14,76 +17,79 @@ // boilerplateConfig will be shown in help output, so please keep it up to // date when you change environment configuration below. -var boilerplateConfig = ` -joyent: - type: joyent - - ## SDC config - - # sdc-user holds the SDC user. It can also be specified in the - # SDC_ACCOUNT environment variable. - # - # sdc-user: - - # sdc-key-id holds the fingerprint of one of user's SSH keys. It - # can also be specified in the SDC_KEY_ID environment variable. - # - # sdc-key-id: - - # sdc-region holds the region to use: us-west-1 (default), - # us-east-1, us-sw-1, eu-ams-1. Override if required. - # - # sdc-region: us-west-1 - - ## Manta config - - # manta-user holds the user's Manta account name. It can also be - # specified in the MANTA_USER environment variable. - # - # manta-user: - - # manta-key-id holds the fingerprint of one of the user's SSH - # keys. It can also be specified in the MANTA_KEY_ID environment - # variable. - # - # manta-key-id: - - # manta-region holds the Manta region to use. It defaults to - # us-east, override if required - # - # manta-region: us-east +const boilerplateConfig = `joyent: + type: joyent -`[1:] + # SDC config + # Can be set via env variables, or specified here + # sdc-user: + # Can be set via env variables, or specified here + # sdc-key-id: + # url defaults to us-west-1 DC, override if required + # sdc-url: https://us-west-1.api.joyentcloud.com + + # Manta config + # Can be set via env variables, or specified here + # manta-user: + # Can be set via env variables, or specified here + # manta-key-id: + # url defaults to us-east DC, override if required + # manta-url: https://us-east.manta.joyent.com + + # Auth config + # private-key-path is the private key used to sign Joyent requests. + # Defaults to ~/.ssh/id_rsa, override if a different ssh key is used. + # Alternatively, you can supply "private-key" with the content of the private + # key instead supplying the path to a file. + # private-key-path: ~/.ssh/id_rsa + # algorithm defaults to rsa-sha256, override if required + # algorithm: rsa-sha256 +` const ( - SdcAccount = "SDC_ACCOUNT" - SdcKeyId = "SDC_KEY_ID" - SdcUrl = "SDC_URL" - MantaUser = "MANTA_USER" - MantaKeyId = "MANTA_KEY_ID" - MantaUrl = "MANTA_URL" + SdcAccount = "SDC_ACCOUNT" + SdcKeyId = "SDC_KEY_ID" + SdcUrl = "SDC_URL" + MantaUser = "MANTA_USER" + MantaKeyId = "MANTA_KEY_ID" + MantaUrl = "MANTA_URL" + MantaPrivateKeyFile = "MANTA_PRIVATE_KEY_FILE" + DefaultPrivateKey = "~/.ssh/id_rsa" ) var environmentVariables = map[string]string{ - "sdc-user": SdcAccount, - "sdc-key-id": SdcKeyId, - "manta-user": MantaUser, - "manta-key-id": MantaKeyId, + "sdc-user": SdcAccount, + "sdc-key-id": SdcKeyId, + "sdc-url": SdcUrl, + "manta-user": MantaUser, + "manta-key-id": MantaKeyId, + "manta-url": MantaUrl, + "private-key-path": MantaPrivateKeyFile, } var configFields = schema.Fields{ - "sdc-user": schema.String(), - "sdc-key-id": schema.String(), - "sdc-region": schema.String(), - "manta-user": schema.String(), - "manta-key-id": schema.String(), - "manta-region": schema.String(), - "control-dir": schema.String(), -} - -var configDefaultFields = schema.Defaults{ - "sdc-region": "us-west-1", - "manta-region": "us-east", + "sdc-user": schema.String(), + "sdc-key-id": schema.String(), + "sdc-url": schema.String(), + "manta-user": schema.String(), + "manta-key-id": schema.String(), + "manta-url": schema.String(), + "private-key-path": schema.String(), + "algorithm": schema.String(), + "control-dir": schema.String(), + "private-key": schema.String(), +} + +var configDefaults = schema.Defaults{ + "sdc-url": "https://us-west-1.api.joyentcloud.com", + "manta-url": "https://us-east.manta.joyent.com", + "algorithm": "rsa-sha256", + "private-key-path": schema.Omit, + "sdc-user": schema.Omit, + "sdc-key-id": schema.Omit, + "manta-user": schema.Omit, + "manta-key-id": schema.Omit, + "private-key": schema.Omit, } var configSecretFields = []string{ @@ -91,11 +97,15 @@ "sdc-key-id", "manta-user", "manta-key-id", + "private-key", } var configImmutableFields = []string{ - "sdc-region", - "manta-region", + "sdc-url", + "manta-url", + "private-key-path", + "private-key", + "algorithm", } func prepareConfig(cfg *config.Config) (*config.Config, error) { @@ -107,78 +117,82 @@ if err != nil { return nil, err } - attrs["control-bucket"] = fmt.Sprintf("%x", uuid.Raw()) - } - - // Read env variables - for _, field := range configSecretFields { - // If field is not set, get it from env variables - if attrs[field] == "" { - localEnvVariable := os.Getenv(environmentVariables[field]) - if localEnvVariable != "" { - attrs[field] = localEnvVariable - } else { - return nil, fmt.Errorf("cannot get %s value from environment variables %s", field, environmentVariables[field]) - } - } + attrs["control-dir"] = fmt.Sprintf("%x", uuid.Raw()) } - return cfg.Apply(attrs) } -func validateConfig(cfg *config.Config, old *environConfig) (*environConfig, error) { - // Check sanity of juju-level fields. - var oldCfg *config.Config - if old != nil { - oldCfg = old.Config - } - if err := config.Validate(cfg, oldCfg); err != nil { +func validateConfig(cfg, old *config.Config) (*environConfig, error) { + // Check for valid changes for the base config values. + if err := config.Validate(cfg, old); err != nil { return nil, err } - // Extract validated provider-specific fields. All of configFields will be - // present in validated, and defaults will be inserted if necessary. If the - // schema you passed in doesn't quite express what you need, you can make - // whatever checks you need here, before continuing. - // In particular, if you want to extract (say) credentials from the user's - // shell environment variables, you'll need to allow missing values to pass - // through the schema by setting a value of schema.Omit in the configFields - // map, and then to set and check them at this point. These values *must* be - // stored in newAttrs: a Config will be generated on the user's machine only - // to begin with, and will subsequently be used on a different machine that - // will probably not have those variables set. - newAttrs, err := cfg.ValidateUnknownAttrs(configFields, configDefaultFields) + newAttrs, err := cfg.ValidateUnknownAttrs(configFields, configDefaults) if err != nil { return nil, err } - for field := range configFields { - if newAttrs[field] == "" { - return nil, fmt.Errorf("%s: must not be empty", field) - } - } - + envConfig := &environConfig{cfg, newAttrs} // If an old config was supplied, check any immutable fields have not changed. if old != nil { + oldEnvConfig, err := validateConfig(old, nil) + if err != nil { + return nil, err + } for _, field := range configImmutableFields { - if old.attrs[field] != newAttrs[field] { + if oldEnvConfig.attrs[field] != envConfig.attrs[field] { return nil, fmt.Errorf( "%s: cannot change from %v to %v", - field, old.attrs[field], newAttrs[field], + field, oldEnvConfig.attrs[field], envConfig.attrs[field], ) } } } - // Merge the validated provider-specific fields into the original config, - // to ensure the object we return is internally consistent. - newCfg, err := cfg.Apply(newAttrs) - if err != nil { - return nil, err + // Read env variables to fill in any missing fields. + for field, envVar := range environmentVariables { + // If field is not set, get it from env variables + if fieldValue, ok := envConfig.attrs[field]; !ok || fieldValue == "" { + localEnvVariable := os.Getenv(envVar) + if localEnvVariable != "" { + envConfig.attrs[field] = localEnvVariable + } else { + if field != "private-key-path" { + return nil, fmt.Errorf("cannot get %s value from environment variable %s", field, envVar) + } + } + } + } + + // Ensure private-key-path is set - if it's not in config or an env var, use a default value. + if v, ok := envConfig.attrs["private-key-path"]; !ok || v == "" { + v = os.Getenv(environmentVariables["private-key-path"]) + if v == "" { + v = DefaultPrivateKey + } + envConfig.attrs["private-key-path"] = v + } + // Now that we've ensured private-key-path is properly set, we go back and set + // up the private key - this is used to sign requests. + if fieldValue, ok := envConfig.attrs["private-key"]; !ok || fieldValue == "" { + keyFile, err := utils.NormalizePath(envConfig.attrs["private-key-path"].(string)) + if err != nil { + return nil, err + } + privateKey, err := ioutil.ReadFile(keyFile) + if err != nil { + return nil, err + } + envConfig.attrs["private-key"] = string(privateKey) + } + + // Check for missing fields. + for field := range configFields { + if envConfig.attrs[field] == "" { + return nil, fmt.Errorf("%s: must not be empty", field) + } } - return &environConfig{ - Config: newCfg, - attrs: newAttrs, - }, nil + return envConfig, nil } type environConfig struct { @@ -186,8 +200,12 @@ attrs map[string]interface{} } -func (ecfg *environConfig) sdcRegion() string { - return ecfg.attrs["sdc-region"].(string) +func (ecfg *environConfig) GetAttrs() map[string]interface{} { + return ecfg.attrs +} + +func (ecfg *environConfig) sdcUrl() string { + return ecfg.attrs["sdc-url"].(string) } func (ecfg *environConfig) sdcUser() string { @@ -198,8 +216,8 @@ return ecfg.attrs["sdc-key-id"].(string) } -func (ecfg *environConfig) mantaRegion() string { - return ecfg.attrs["manta-region"].(string) +func (ecfg *environConfig) mantaUrl() string { + return ecfg.attrs["manta-url"].(string) } func (ecfg *environConfig) mantaUser() string { @@ -210,6 +228,46 @@ return ecfg.attrs["manta-key-id"].(string) } +func (ecfg *environConfig) privateKey() string { + if v, ok := ecfg.attrs["private-key"]; ok { + return v.(string) + } + return "" +} + +func (ecfg *environConfig) algorithm() string { + return ecfg.attrs["algorithm"].(string) +} + func (c *environConfig) controlDir() string { return c.attrs["control-dir"].(string) } + +func (c *environConfig) ControlDir() string { + return c.controlDir() +} + +func (ecfg *environConfig) SdcUrl() string { + return ecfg.sdcUrl() +} + +func (ecfg *environConfig) Region() string { + sdcUrl := ecfg.sdcUrl() + // Check if running against local services + if isLocalhost(sdcUrl) { + return "some-region" + } + return sdcUrl[strings.LastIndex(sdcUrl, "/")+1 : strings.Index(sdcUrl, ".")] +} + +func isLocalhost(u string) bool { + parsedUrl, err := url.Parse(u) + if err != nil { + return false + } + if strings.HasPrefix(parsedUrl.Host, "localhost") || strings.HasPrefix(parsedUrl.Host, "127.0.0.") { + return true + } + + return false +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/config_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/config_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/config_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/config_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -4,110 +4,212 @@ package joyent_test import ( + "fmt" + "io/ioutil" + "os" + "github.com/juju/testing" gc "launchpad.net/gocheck" "launchpad.net/juju-core/environs" "launchpad.net/juju-core/environs/config" - "launchpad.net/juju-core/provider/joyent" + jp "launchpad.net/juju-core/provider/joyent" coretesting "launchpad.net/juju-core/testing" - "launchpad.net/juju-core/testing/testbase" + "launchpad.net/juju-core/utils" ) func newConfig(c *gc.C, attrs coretesting.Attrs) *config.Config { attrs = coretesting.FakeConfig().Merge(attrs) - cfg, err := config.New(config.NoDefaults, attrs) + cfg, err := config.New(config.UseDefaults, attrs) c.Assert(err, gc.IsNil) return cfg } func validAttrs() coretesting.Attrs { return coretesting.FakeConfig().Merge(coretesting.Attrs{ - "type": "joyent", - "sdc-user": "dstroppa", - "sdc-key-id": "12:c3:a7:cb:a2:29:e2:90:88:3f:04:53:3b:4e:75:40", - "sdc-region": "us-west-1", - "manta-user": "dstroppa", - "manta-key-id": "12:c3:a7:cb:a2:29:e2:90:88:3f:04:53:3b:4e:75:40", - "manta-region": "us-east", - "control-dir": "juju-test", + "type": "joyent", + "sdc-user": "juju-test", + "sdc-key-id": "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff", + "sdc-url": "https://test.api.joyentcloud.com", + "manta-user": "juju-test", + "manta-key-id": "00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff", + "manta-url": "https://test.manta.joyent.com", + "private-key-path": "~/.ssh/provider_id_rsa", + "algorithm": "rsa-sha256", + "control-dir": "juju-test", + "private-key": "key", }) } type ConfigSuite struct { - testbase.LoggingSuite + coretesting.FakeHomeSuite originalValues map[string]testing.Restorer } var _ = gc.Suite(&ConfigSuite{}) func (s *ConfigSuite) SetUpSuite(c *gc.C) { - s.PatchEnvironment(joyent.SdcAccount, "tester") - s.PatchEnvironment(joyent.SdcKeyId, "11:c4:b6:c0:a3:24:22:96:a8:1f:07:53:3f:8e:14:7a") - s.PatchEnvironment(joyent.MantaUser, "tester") - s.PatchEnvironment(joyent.MantaKeyId, "11:c4:b6:c0:a3:24:22:96:a8:1f:07:53:3f:8e:14:7a") + s.FakeHomeSuite.SetUpSuite(c) + restoreSdcAccount := testing.PatchEnvironment(jp.SdcAccount, "tester") + s.AddSuiteCleanup(func(*gc.C) { restoreSdcAccount() }) + restoreSdcKeyId := testing.PatchEnvironment(jp.SdcKeyId, "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00") + s.AddSuiteCleanup(func(*gc.C) { restoreSdcKeyId() }) + restoreMantaUser := testing.PatchEnvironment(jp.MantaUser, "tester") + s.AddSuiteCleanup(func(*gc.C) { restoreMantaUser() }) + restoreMantaKeyId := testing.PatchEnvironment(jp.MantaKeyId, "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00") + s.AddSuiteCleanup(func(*gc.C) { restoreMantaKeyId() }) +} + +func (s *ConfigSuite) SetUpTest(c *gc.C) { + s.FakeHomeSuite.SetUpTest(c) + s.AddCleanup(CreateTestKey(c)) + for _, envVar := range jp.EnvironmentVariables { + s.PatchEnvironment(envVar, "") + } } var newConfigTests = []struct { - info string - insert coretesting.Attrs - remove []string - expect coretesting.Attrs - err string + info string + insert coretesting.Attrs + remove []string + envVars map[string]string + expect coretesting.Attrs + err string }{{ info: "sdc-user is required", remove: []string{"sdc-user"}, - err: "sdc-user: expected string, got nothing", + err: ".* cannot get sdc-user value from environment variable .*", }, { info: "sdc-user cannot be empty", insert: coretesting.Attrs{"sdc-user": ""}, - err: "sdc-user: must not be empty", + err: ".* cannot get sdc-user value from environment variable .*", +}, { + info: "can get sdc-user from env variable", + insert: coretesting.Attrs{"sdc-user": ""}, + expect: coretesting.Attrs{"sdc-user": "tester"}, + envVars: map[string]string{ + "SDC_ACCOUNT": "tester", + }, +}, { + info: "can get sdc-user from env variable, missing from config", + remove: []string{"sdc-user"}, + expect: coretesting.Attrs{"sdc-user": "tester"}, + envVars: map[string]string{ + "SDC_ACCOUNT": "tester", + }, }, { info: "sdc-key-id is required", remove: []string{"sdc-key-id"}, - err: "sdc-key-id: expected string, got nothing", + err: ".* cannot get sdc-key-id value from environment variable .*", }, { info: "sdc-key-id cannot be empty", insert: coretesting.Attrs{"sdc-key-id": ""}, - err: "sdc-key-id: must not be empty", + err: ".* cannot get sdc-key-id value from environment variable .*", }, { - info: "sdc-region is inserted if missing", - expect: coretesting.Attrs{"sdc-region": "us-west-1"}, + info: "can get sdc-key-id from env variable", + insert: coretesting.Attrs{"sdc-key-id": ""}, + expect: coretesting.Attrs{"sdc-key-id": "key"}, + envVars: map[string]string{ + "SDC_KEY_ID": "key", + }, }, { - info: "sdc-region cannot be empty", - insert: coretesting.Attrs{"sdc-region": ""}, - err: "sdc-region: must not be empty", -}, { - info: "sdc-region is untouched if present", - insert: coretesting.Attrs{"sdc-region": "us-west-1"}, - expect: coretesting.Attrs{"sdc-region": "us-west-1"}, + info: "can get sdc-key-id from env variable, missing from config", + remove: []string{"sdc-key-id"}, + expect: coretesting.Attrs{"sdc-key-id": "key"}, + envVars: map[string]string{ + "SDC_KEY_ID": "key", + }, +}, { + info: "sdc-url is inserted if missing", + expect: coretesting.Attrs{"sdc-url": "https://test.api.joyentcloud.com"}, +}, { + info: "sdc-url cannot be empty", + insert: coretesting.Attrs{"sdc-url": ""}, + err: ".* cannot get sdc-url value from environment variable .*", +}, { + info: "sdc-url is untouched if present", + insert: coretesting.Attrs{"sdc-url": "https://test.api.joyentcloud.com"}, + expect: coretesting.Attrs{"sdc-url": "https://test.api.joyentcloud.com"}, }, { info: "manta-user is required", remove: []string{"manta-user"}, - err: "manta-user: expected string, got nothing", + err: ".* cannot get manta-user value from environment variable .*", }, { info: "manta-user cannot be empty", insert: coretesting.Attrs{"manta-user": ""}, - err: "manta-user: must not be empty", + err: ".* cannot get manta-user value from environment variable .*", +}, { + info: "can get manta-user from env variable", + insert: coretesting.Attrs{"manta-user": ""}, + expect: coretesting.Attrs{"manta-user": "tester"}, + envVars: map[string]string{ + "MANTA_USER": "tester", + }, +}, { + info: "can get manta-user from env variable, missing from config", + remove: []string{"manta-user"}, + expect: coretesting.Attrs{"manta-user": "tester"}, + envVars: map[string]string{ + "MANTA_USER": "tester", + }, }, { info: "manta-key-id is required", remove: []string{"manta-key-id"}, - err: "manta-key-id: expected string, got nothing", + err: ".* cannot get manta-key-id value from environment variable .*", }, { info: "manta-key-id cannot be empty", insert: coretesting.Attrs{"manta-key-id": ""}, - err: "manta-key-id: must not be empty", + err: ".* cannot get manta-key-id value from environment variable .*", }, { - info: "manta-region is inserted if missing", - expect: coretesting.Attrs{"manta-region": "us-east"}, + info: "can get manta-key-id from env variable", + insert: coretesting.Attrs{"manta-key-id": ""}, + expect: coretesting.Attrs{"manta-key-id": "key"}, + envVars: map[string]string{ + "MANTA_KEY_ID": "key", + }, }, { - info: "manta-region cannot be empty", - insert: coretesting.Attrs{"manta-region": ""}, - err: "manta-region: must not be empty", -}, { - info: "manta-region is untouched if present", - insert: coretesting.Attrs{"manta-region": "us-east"}, - expect: coretesting.Attrs{"manta-region": "us-east"}, + info: "can get manta-key-id from env variable, missing from config", + remove: []string{"manta-key-id"}, + expect: coretesting.Attrs{"manta-key-id": "key"}, + envVars: map[string]string{ + "MANTA_KEY_ID": "key", + }, +}, { + info: "manta-url is inserted if missing", + expect: coretesting.Attrs{"manta-url": "https://test.manta.joyent.com"}, +}, { + info: "manta-url cannot be empty", + insert: coretesting.Attrs{"manta-url": ""}, + err: ".* cannot get manta-url value from environment variable .*", +}, { + info: "manta-url is untouched if present", + insert: coretesting.Attrs{"manta-url": "https://test.manta.joyent.com"}, + expect: coretesting.Attrs{"manta-url": "https://test.manta.joyent.com"}, +}, { + info: "private-key-path is inserted if missing", + remove: []string{"private-key-path"}, + expect: coretesting.Attrs{"private-key-path": "~/.ssh/id_rsa"}, +}, { + info: "can get private-key-path from env variable", + insert: coretesting.Attrs{"private-key-path": ""}, + expect: coretesting.Attrs{"private-key-path": "some-file"}, + envVars: map[string]string{ + "MANTA_PRIVATE_KEY_FILE": "some-file", + }, +}, { + info: "can get private-key-path from env variable, missing from config", + remove: []string{"private-key-path"}, + expect: coretesting.Attrs{"private-key-path": "some-file"}, + envVars: map[string]string{ + "MANTA_PRIVATE_KEY_FILE": "some-file", + }, +}, { + info: "algorithm is inserted if missing", + expect: coretesting.Attrs{"algorithm": "rsa-sha256"}, +}, { + info: "algorithm cannot be empty", + insert: coretesting.Attrs{"algorithm": ""}, + err: ".* algorithm: must not be empty", }, { info: "unknown field is not touched", insert: coretesting.Attrs{"unknown-field": 12345}, @@ -117,61 +219,25 @@ func (*ConfigSuite) TestNewEnvironConfig(c *gc.C) { for i, test := range newConfigTests { c.Logf("test %d: %s", i, test.info) + for k, v := range test.envVars { + os.Setenv(k, v) + } attrs := validAttrs().Merge(test.insert).Delete(test.remove...) testConfig := newConfig(c, attrs) environ, err := environs.New(testConfig) if test.err == "" { - c.Assert(err, gc.IsNil) + c.Check(err, gc.IsNil) attrs := environ.Config().AllAttrs() for field, value := range test.expect { c.Check(attrs[field], gc.Equals, value) } } else { - c.Assert(environ, gc.IsNil) + c.Check(environ, gc.IsNil) c.Check(err, gc.ErrorMatches, test.err) } } } -func (*ConfigSuite) TestValidateNewConfig(c *gc.C) { - for i, test := range newConfigTests { - c.Logf("test %d: %s", i, test.info) - attrs := validAttrs().Merge(test.insert).Delete(test.remove...) - testConfig := newConfig(c, attrs) - validatedConfig, err := joyent.Provider.Validate(testConfig, nil) - if test.err == "" { - c.Assert(err, gc.IsNil) - attrs := validatedConfig.AllAttrs() - for field, value := range test.expect { - c.Check(attrs[field], gc.Equals, value) - } - } else { - c.Assert(validatedConfig, gc.IsNil) - c.Check(err, gc.ErrorMatches, "invalid Joyent provider config: "+test.err) - } - } -} - -func (*ConfigSuite) TestValidateOldConfig(c *gc.C) { - knownGoodConfig := newConfig(c, validAttrs()) - for i, test := range newConfigTests { - c.Logf("test %d: %s", i, test.info) - attrs := validAttrs().Merge(test.insert).Delete(test.remove...) - testConfig := newConfig(c, attrs) - validatedConfig, err := joyent.Provider.Validate(knownGoodConfig, testConfig) - if test.err == "" { - c.Assert(err, gc.IsNil) - attrs := validatedConfig.AllAttrs() - for field, value := range validAttrs() { - c.Check(attrs[field], gc.Equals, value) - } - } else { - c.Assert(validatedConfig, gc.IsNil) - c.Check(err, gc.ErrorMatches, "original Joyent provider config is invalid: "+test.err) - } - } -} - var changeConfigTests = []struct { info string insert coretesting.Attrs @@ -187,24 +253,24 @@ expect: coretesting.Attrs{"sdc-user": "joyent_user"}, }, { info: "can change sdc-key-id", - insert: coretesting.Attrs{"sdc-key-id": "11:c4:b6:c0:a3:24:22:96:a8:1f:07:53:3f:8e:14:7a"}, - expect: coretesting.Attrs{"sdc-key-id": "11:c4:b6:c0:a3:24:22:96:a8:1f:07:53:3f:8e:14:7a"}, + insert: coretesting.Attrs{"sdc-key-id": "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00"}, + expect: coretesting.Attrs{"sdc-key-id": "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00"}, }, { - info: "can change sdc-region", - insert: coretesting.Attrs{"sdc-region": "us-west-1"}, - expect: coretesting.Attrs{"sdc-region": "us-west-1"}, + info: "can change sdc-url", + insert: coretesting.Attrs{"sdc-url": "https://test.api.joyentcloud.com"}, + expect: coretesting.Attrs{"sdc-url": "https://test.api.joyentcloud.com"}, }, { info: "can change manta-user", insert: coretesting.Attrs{"manta-user": "manta_user"}, expect: coretesting.Attrs{"manta-user": "manta_user"}, }, { info: "can change manta-key-id", - insert: coretesting.Attrs{"manta-key-id": "11:c4:b6:c0:a3:24:22:96:a8:1f:07:53:3f:8e:14:7a"}, - expect: coretesting.Attrs{"manta-key-id": "11:c4:b6:c0:a3:24:22:96:a8:1f:07:53:3f:8e:14:7a"}, + insert: coretesting.Attrs{"manta-key-id": "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00"}, + expect: coretesting.Attrs{"manta-key-id": "ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00"}, }, { - info: "can change manta-region", - insert: coretesting.Attrs{"manta-region": "us-east"}, - expect: coretesting.Attrs{"manta-region": "us-east"}, + info: "can change manta-url", + insert: coretesting.Attrs{"manta-url": "https://test.manta.joyent.com"}, + expect: coretesting.Attrs{"manta-url": "https://test.manta.joyent.com"}, }, { info: "can insert unknown field", insert: coretesting.Attrs{"unknown": "ignoti"}, @@ -217,15 +283,15 @@ c.Logf("test %d: %s", i, test.info) attrs := validAttrs().Merge(test.insert).Delete(test.remove...) testConfig := newConfig(c, attrs) - validatedConfig, err := joyent.Provider.Validate(testConfig, baseConfig) + validatedConfig, err := jp.Provider.Validate(testConfig, baseConfig) if test.err == "" { - c.Assert(err, gc.IsNil) + c.Check(err, gc.IsNil) attrs := validatedConfig.AllAttrs() for field, value := range test.expect { c.Check(attrs[field], gc.Equals, value) } } else { - c.Assert(validatedConfig, gc.IsNil) + c.Check(validatedConfig, gc.IsNil) c.Check(err, gc.ErrorMatches, "invalid config change: "+test.err) } } @@ -242,7 +308,7 @@ err = environ.SetConfig(testConfig) newAttrs := environ.Config().AllAttrs() if test.err == "" { - c.Assert(err, gc.IsNil) + c.Check(err, gc.IsNil) for field, value := range test.expect { c.Check(newAttrs[field], gc.Equals, value) } @@ -255,6 +321,10 @@ } } +func validPrepareAttrs() coretesting.Attrs { + return validAttrs().Delete("private-key") +} + var prepareConfigTests = []struct { info string insert coretesting.Attrs @@ -263,40 +333,50 @@ err string }{{ info: "All value provided, nothig to do", - expect: validAttrs(), + expect: validPrepareAttrs(), }, { - info: "can get sdc-user from env variable", - insert: coretesting.Attrs{"sdc-user": ""}, - expect: coretesting.Attrs{"sdc-user": "tester"}, -}, { - info: "can get sdc-key-id from env variable", - insert: coretesting.Attrs{"sdc-key-id": ""}, - expect: coretesting.Attrs{"sdc-key-id": "11:c4:b6:c0:a3:24:22:96:a8:1f:07:53:3f:8e:14:7a"}, -}, { - info: "can get manta-user from env variable", - insert: coretesting.Attrs{"manta-user": ""}, - expect: coretesting.Attrs{"manta-user": "tester"}, -}, { - info: "can get manta-key-id from env variable", - insert: coretesting.Attrs{"manta-key-id": ""}, - expect: coretesting.Attrs{"manta-key-id": "11:c4:b6:c0:a3:24:22:96:a8:1f:07:53:3f:8e:14:7a"}, + info: "private key is loaded from key file", + insert: coretesting.Attrs{"private-key-path": fmt.Sprintf("~/.ssh/%s", testKeyFileName)}, + expect: coretesting.Attrs{"private-key": testPrivateKey}, +}, { + info: "bad private-key-path errors, not panics", + insert: coretesting.Attrs{"private-key-path": "~/.ssh/no-such-file"}, + err: "invalid Joyent provider config: open .*: no such file or directory", }} func (s *ConfigSuite) TestPrepare(c *gc.C) { + ctx := coretesting.Context(c) for i, test := range prepareConfigTests { c.Logf("test %d: %s", i, test.info) - attrs := validAttrs().Merge(test.insert).Delete(test.remove...) + attrs := validPrepareAttrs().Merge(test.insert).Delete(test.remove...) testConfig := newConfig(c, attrs) - preparedConfig, err := joyent.Provider.Prepare(coretesting.Context(c), testConfig) + preparedConfig, err := jp.Provider.Prepare(ctx, testConfig) if test.err == "" { - c.Assert(err, gc.IsNil) + c.Check(err, gc.IsNil) attrs := preparedConfig.Config().AllAttrs() for field, value := range test.expect { c.Check(attrs[field], gc.Equals, value) } } else { - c.Assert(preparedConfig, gc.IsNil) - c.Check(err, gc.ErrorMatches, "invalid prepare config: "+test.err) + c.Check(preparedConfig, gc.IsNil) + c.Check(err, gc.ErrorMatches, test.err) } } } + +func (s *ConfigSuite) TestPrepareWithDefaultKeyFile(c *gc.C) { + ctx := coretesting.Context(c) + // By default "private-key-path isn't set until after validateConfig has been called. + attrs := validAttrs().Delete("private-key-path", "private-key") + keyFilePath, err := utils.NormalizePath(jp.DefaultPrivateKey) + c.Assert(err, gc.IsNil) + err = ioutil.WriteFile(keyFilePath, []byte(testPrivateKey), 400) + c.Assert(err, gc.IsNil) + defer os.Remove(keyFilePath) + testConfig := newConfig(c, attrs) + preparedConfig, err := jp.Provider.Prepare(ctx, testConfig) + c.Assert(err, gc.IsNil) + attrs = preparedConfig.Config().AllAttrs() + c.Check(attrs["private-key-path"], gc.Equals, jp.DefaultPrivateKey) + c.Check(attrs["private-key"], gc.Equals, testPrivateKey) +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/environ_firewall.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/environ_firewall.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/environ_firewall.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/environ_firewall.go 2014-04-04 16:53:35.000000000 +0000 @@ -4,26 +4,128 @@ package joyent import ( + "fmt" + "strconv" + "strings" + + "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/instance" + + "github.com/joyent/gosdc/cloudapi" +) + +const ( + firewallRuleAll = "FROM tag %s TO tag juju ALLOW %s PORT %d" ) -// Implementing the methods below (to do something other than return nil) will -// cause `juju expose` to work when the firewall-mode is "global". If you -// implement one of them, you should implement them all. - -func (env *environ) OpenPorts(ports []instance.Port) error { - logger.Warningf("pretending to open ports %v for all instances", ports) - _ = env.getSnapshot() +// Helper method to create a firewall rule string for the given port +func createFirewallRuleAll(env *joyentEnviron, port instance.Port) string { + return fmt.Sprintf(firewallRuleAll, env.Name(), strings.ToLower(port.Protocol), port.Number) +} + +// Helper method to check if a firewall rule string already exist +func ruleExists(rules []cloudapi.FirewallRule, rule string) (bool, string) { + for _, r := range rules { + if strings.EqualFold(r.Rule, rule) { + return true, r.Id + } + } + + return false, "" +} + +// Helper method to get port from the given firewall rules +func getPorts(env *joyentEnviron, rules []cloudapi.FirewallRule) []instance.Port { + ports := []instance.Port{} + for _, r := range rules { + rule := r.Rule + if r.Enabled && strings.HasPrefix(rule, "FROM tag "+env.Name()) && strings.Contains(rule, "PORT") { + p := rule[strings.Index(rule, "ALLOW")+6 : strings.Index(rule, "PORT")-1] + n, _ := strconv.Atoi(rule[strings.LastIndex(rule, " ")+1:]) + port := instance.Port{Protocol: p, Number: n} + ports = append(ports, port) + } + } + + instance.SortPorts(ports) + return ports +} + +func (env *joyentEnviron) OpenPorts(ports []instance.Port) error { + if env.Config().FirewallMode() != config.FwGlobal { + return fmt.Errorf("invalid firewall mode %q for opening ports on environment", env.Config().FirewallMode()) + } + + fwRules, err := env.compute.cloudapi.ListFirewallRules() + if err != nil { + return fmt.Errorf("cannot get firewall rules: %v", err) + } + + for _, p := range ports { + rule := createFirewallRuleAll(env, p) + if e, id := ruleExists(fwRules, rule); e { + _, err := env.compute.cloudapi.EnableFirewallRule(id) + if err != nil { + return fmt.Errorf("couldn't enable rule %s: %v", rule, err) + } + } else { + _, err := env.compute.cloudapi.CreateFirewallRule(cloudapi.CreateFwRuleOpts{ + Enabled: true, + Rule: rule, + }) + if err != nil { + return fmt.Errorf("couldn't create rule %s: %v", rule, err) + } + } + } + + logger.Infof("ports %v opened in environment", ports) + return nil } -func (env *environ) ClosePorts(ports []instance.Port) error { - logger.Warningf("pretending to close ports %v for all instances", ports) - _ = env.getSnapshot() +func (env *joyentEnviron) ClosePorts(ports []instance.Port) error { + if env.Config().FirewallMode() != config.FwGlobal { + return fmt.Errorf("invalid firewall mode %q for closing ports on environment", env.Config().FirewallMode()) + } + + fwRules, err := env.compute.cloudapi.ListFirewallRules() + if err != nil { + return fmt.Errorf("cannot get firewall rules: %v", err) + } + + for _, p := range ports { + rule := createFirewallRuleAll(env, p) + if e, id := ruleExists(fwRules, rule); e { + _, err := env.compute.cloudapi.DisableFirewallRule(id) + if err != nil { + return fmt.Errorf("couldn't disable rule %s: %v", rule, err) + } + } else { + _, err := env.compute.cloudapi.CreateFirewallRule(cloudapi.CreateFwRuleOpts{ + Enabled: false, + Rule: rule, + }) + if err != nil { + return fmt.Errorf("couldn't create rule %s: %v", rule, err) + } + } + } + + logger.Infof("ports %v closed in environment", ports) + return nil } -func (env *environ) Ports() ([]instance.Port, error) { - _ = env.getSnapshot() - return nil, nil +func (env *joyentEnviron) Ports() ([]instance.Port, error) { + if env.Config().FirewallMode() != config.FwGlobal { + return nil, fmt.Errorf("invalid firewall mode %q for retrieving ports from environment", env.Config().FirewallMode()) + } + + fwRules, err := env.compute.cloudapi.ListFirewallRules() + if err != nil { + return nil, fmt.Errorf("cannot get firewall rules: %v", err) + } + + return getPorts(env, fwRules), nil } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/environ.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/environ.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/environ.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/environ.go 2014-04-04 16:57:31.000000000 +0000 @@ -17,19 +17,14 @@ "launchpad.net/juju-core/state/api" ) -// This file contains the core of the joyent Environ implementation. You will -// probably not need to change this file very much to begin with; and if you -// never need to add any more fields, you may never need to touch it. -// -// The rest of the implementation is split into environ_instance.go (which -// must be implemented ) and environ_firewall.go (which can be safely -// ignored until you've got an environment bootstrapping successfully). +// This file contains the core of the Joyent Environ implementation. -type environ struct { +type joyentEnviron struct { name string // supportedArchitectures caches the architectures // for which images can be instantiated. + archLock sync.Mutex supportedArchitectures []string // All mutating operations should lock the mutex. Non-mutating operations @@ -40,57 +35,76 @@ lock sync.Mutex ecfg *environConfig storage storage.Storage + compute *joyentCompute } -var _ environs.Environ = (*environ)(nil) +var _ environs.Environ = (*joyentEnviron)(nil) -func (env *environ) Name() string { +// newEnviron create a new Joyent environ instance from config. +func newEnviron(cfg *config.Config) (*joyentEnviron, error) { + env := new(joyentEnviron) + if err := env.SetConfig(cfg); err != nil { + return nil, err + } + env.name = cfg.Name() + var err error + env.storage, err = newStorage(env.ecfg, "") + if err != nil { + return nil, err + } + env.compute, err = newCompute(env.ecfg) + if err != nil { + return nil, err + } + return env, nil +} + +func (env *joyentEnviron) SetName(envName string) { + env.name = envName +} + +func (env *joyentEnviron) Name() string { return env.name } -func (*environ) Provider() environs.EnvironProvider { +func (*joyentEnviron) Provider() environs.EnvironProvider { return providerInstance } // SupportedArchitectures is specified on the EnvironCapability interface. -func (e *environ) SupportedArchitectures() ([]string, error) { - e.lock.Lock() - defer e.lock.Unlock() - if e.supportedArchitectures != nil { - return e.supportedArchitectures, nil +func (env *joyentEnviron) SupportedArchitectures() ([]string, error) { + env.archLock.Lock() + defer env.archLock.Unlock() + if env.supportedArchitectures != nil { + return env.supportedArchitectures, nil } + cfg := env.Ecfg() // Create a filter to get all images from our region and for the correct stream. cloudSpec := simplestreams.CloudSpec{ - // TODO - add in region and url when known. - // Region: e.Config().sdcRegion(), - // Endpoint: "", + Region: cfg.Region(), + Endpoint: cfg.SdcUrl(), } imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{ CloudSpec: cloudSpec, - Stream: e.Config().ImageStream(), + Stream: cfg.ImageStream(), }) var err error - e.supportedArchitectures, err = common.SupportedArchitectures(e, imageConstraint) - return e.supportedArchitectures, err + env.supportedArchitectures, err = common.SupportedArchitectures(env, imageConstraint) + return env.supportedArchitectures, err } -func (env *environ) SetConfig(cfg *config.Config) error { +func (env *joyentEnviron) SetConfig(cfg *config.Config) error { env.lock.Lock() defer env.lock.Unlock() - ecfg, err := validateConfig(cfg, env.ecfg) - if err != nil { - return err - } - storage, err := newStorage(ecfg) + ecfg, err := providerInstance.newConfig(cfg) if err != nil { return err } env.ecfg = ecfg - env.storage = storage return nil } -func (env *environ) getSnapshot() *environ { +func (env *joyentEnviron) getSnapshot() *joyentEnviron { env.lock.Lock() clone := *env env.lock.Unlock() @@ -98,26 +112,67 @@ return &clone } -func (env *environ) Config() *config.Config { +func (env *joyentEnviron) Config() *config.Config { return env.getSnapshot().ecfg.Config } -func (env *environ) Storage() storage.Storage { +func (env *joyentEnviron) Storage() storage.Storage { return env.getSnapshot().storage } -func (env *environ) PublicStorage() storage.StorageReader { +func (env *joyentEnviron) PublicStorage() storage.StorageReader { return environs.EmptyStorage } -func (env *environ) Bootstrap(ctx environs.BootstrapContext, cons constraints.Value) error { +func (env *joyentEnviron) Bootstrap(ctx environs.BootstrapContext, cons constraints.Value) error { return common.Bootstrap(ctx, env, cons) } -func (env *environ) StateInfo() (*state.Info, *api.Info, error) { +func (env *joyentEnviron) StateInfo() (*state.Info, *api.Info, error) { return common.StateInfo(env) } -func (env *environ) Destroy() error { +func (env *joyentEnviron) Destroy() error { return common.Destroy(env) } + +func (env *joyentEnviron) Ecfg() *environConfig { + return env.getSnapshot().ecfg +} + +// MetadataLookupParams returns parameters which are used to query simplestreams metadata. +func (env *joyentEnviron) MetadataLookupParams(region string) (*simplestreams.MetadataLookupParams, error) { + if region == "" { + region = env.Ecfg().Region() + } + return &simplestreams.MetadataLookupParams{ + Series: config.PreferredSeries(env.Ecfg()), + Region: region, + Endpoint: env.Ecfg().sdcUrl(), + Architectures: []string{"amd64", "armhf"}, + }, nil +} + +// Region is specified in the HasRegion interface. +func (env *joyentEnviron) Region() (simplestreams.CloudSpec, error) { + return simplestreams.CloudSpec{ + Region: env.Ecfg().Region(), + Endpoint: env.Ecfg().sdcUrl(), + }, nil +} + +// GetImageSources returns a list of sources which are used to search for simplestreams image metadata. +func (env *joyentEnviron) GetImageSources() ([]simplestreams.DataSource, error) { + // Add the simplestreams source off the control bucket. + sources := []simplestreams.DataSource{ + storage.NewStorageSimpleStreamsDataSource("cloud storage", env.Storage(), storage.BaseImagesPath)} + return sources, nil +} + +// GetToolsSources returns a list of sources which are used to search for simplestreams tools metadata. +func (env *joyentEnviron) GetToolsSources() ([]simplestreams.DataSource, error) { + // Add the simplestreams source off the control bucket. + sources := []simplestreams.DataSource{ + storage.NewStorageSimpleStreamsDataSource("cloud storage", env.Storage(), storage.BaseToolsPath)} + return sources, nil +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/environ_instance.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/environ_instance.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/environ_instance.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/environ_instance.go 2014-04-04 16:57:31.000000000 +0000 @@ -4,40 +4,256 @@ package joyent import ( + "fmt" + "strings" + "sync" + "time" + + "github.com/joyent/gocommon/client" + "github.com/joyent/gosdc/cloudapi" + "launchpad.net/juju-core/environs" + "launchpad.net/juju-core/environs/imagemetadata" + "launchpad.net/juju-core/environs/instances" + "launchpad.net/juju-core/environs/simplestreams" "launchpad.net/juju-core/instance" + "launchpad.net/juju-core/names" + "launchpad.net/juju-core/tools" + "launchpad.net/juju-core/utils" +) + +var ( + vTypeSmartmachine = "smartmachine" + vTypeVirtualmachine = "kvm" + signedImageDataOnly = false ) -func (env *environ) StartInstance(args environs.StartInstanceParams) ( - instance.Instance, *instance.HardwareCharacteristics, error, -) { - // Please note that in order to fulfil the demands made of Instances and - // AllInstances, it is imperative that some environment feature be used to - // keep track of which instances were actually started by juju. - _ = env.getSnapshot() - return nil, nil, errNotImplemented -} - -func (env *environ) AllInstances() ([]instance.Instance, error) { - // Please note that this must *not* return instances that have not been - // allocated as part of this environment -- if it does, juju will see they - // are not tracked in state, assume they're stale/rogue, and shut them down. - _ = env.getSnapshot() - return nil, errNotImplemented -} - -func (env *environ) Instances(ids []instance.Id) ([]instance.Instance, error) { - // Please note that this must *not* return instances that have not been - // allocated as part of this environment -- if it does, juju will see they - // are not tracked in state, assume they're stale/rogue, and shut them down. - // This advice applies even if an instance id passed in corresponds to a - // real instance that's not part of the environment -- the Environ should - // treat that no differently to a request for one that does not exist. - _ = env.getSnapshot() - return nil, errNotImplemented -} - -func (env *environ) StopInstances(instances []instance.Instance) error { - _ = env.getSnapshot() - return errNotImplemented +type joyentCompute struct { + sync.Mutex + ecfg *environConfig + cloudapi *cloudapi.Client +} + +func newCompute(cfg *environConfig) (*joyentCompute, error) { + creds, err := credentials(cfg) + if err != nil { + return nil, err + } + client := client.NewClient(cfg.sdcUrl(), cloudapi.DefaultAPIVersion, creds, &logger) + + return &joyentCompute{ + ecfg: cfg, + cloudapi: cloudapi.New(client)}, nil +} + +func (env *joyentEnviron) machineFullName(machineId string) string { + return fmt.Sprintf("juju-%s-%s", env.Name(), names.MachineTag(machineId)) +} + +func (env *joyentEnviron) StartInstance(args environs.StartInstanceParams) (instance.Instance, *instance.HardwareCharacteristics, error) { + + series := args.Tools.OneSeries() + arches := args.Tools.Arches() + spec, err := env.FindInstanceSpec(&instances.InstanceConstraint{ + Region: env.Ecfg().Region(), + Series: series, + Arches: arches, + Constraints: args.Constraints, + }) + if err != nil { + return nil, nil, err + } + tools, err := args.Tools.Match(tools.Filter{Arch: spec.Image.Arch}) + if err != nil { + return nil, nil, fmt.Errorf("chosen architecture %v not present in %v", spec.Image.Arch, arches) + } + + args.MachineConfig.Tools = tools[0] + + if err := environs.FinishMachineConfig(args.MachineConfig, env.Config(), args.Constraints); err != nil { + return nil, nil, err + } + userData, err := environs.ComposeUserData(args.MachineConfig, nil) + if err != nil { + return nil, nil, fmt.Errorf("cannot make user data: %v", err) + } + + // Unzipping as Joyent API expects it as string + userData, err = utils.Gunzip(userData) + if err != nil { + return nil, nil, fmt.Errorf("cannot make user data: %v", err) + } + logger.Debugf("joyent user data: %d bytes", len(userData)) + + var machine *cloudapi.Machine + machine, err = env.compute.cloudapi.CreateMachine(cloudapi.CreateMachineOpts{ + //Name: env.machineFullName(machineConf.MachineId), + Package: spec.InstanceType.Name, + Image: spec.Image.Id, + Metadata: map[string]string{"metadata.cloud-init:user-data": string(userData)}, + Tags: map[string]string{"tag.group": "juju", "tag.env": env.Name()}, + }) + if err != nil { + return nil, nil, fmt.Errorf("cannot create instances: %v", err) + } + machineId := machine.Id + + logger.Infof("provisioning instance %q", machineId) + + machine, err = env.compute.cloudapi.GetMachine(machineId) + if err != nil { + return nil, nil, fmt.Errorf("cannot start instances: %v", err) + } + + // wait for machine to start + for !strings.EqualFold(machine.State, "running") { + time.Sleep(1 * time.Second) + + machine, err = env.compute.cloudapi.GetMachine(machineId) + if err != nil { + return nil, nil, fmt.Errorf("cannot start instances: %v", err) + } + } + + logger.Infof("started instance %q", machineId) + + inst := &joyentInstance{ + machine: machine, + env: env, + } + + disk64 := uint64(machine.Disk) + hc := instance.HardwareCharacteristics{ + Arch: &spec.Image.Arch, + Mem: &spec.InstanceType.Mem, + CpuCores: &spec.InstanceType.CpuCores, + CpuPower: spec.InstanceType.CpuPower, + RootDisk: &disk64, + } + + return inst, &hc, nil +} + +func (env *joyentEnviron) AllInstances() ([]instance.Instance, error) { + instances := []instance.Instance{} + + filter := cloudapi.NewFilter() + filter.Set("tag.group", "juju") + filter.Set("tag.env", env.Name()) + + machines, err := env.compute.cloudapi.ListMachines(filter) + if err != nil { + return nil, fmt.Errorf("cannot retrieve instances: %v", err) + } + + for _, m := range machines { + if strings.EqualFold(m.State, "provisioning") || strings.EqualFold(m.State, "running") { + copy := m + instances = append(instances, &joyentInstance{machine: ©, env: env}) + } + } + + return instances, nil +} + +func (env *joyentEnviron) Instances(ids []instance.Id) ([]instance.Instance, error) { + if len(ids) == 0 { + return nil, nil + } + + logger.Debugf("Looking for instances %q", ids) + + instances := make([]instance.Instance, len(ids)) + found := 0 + + allInstances, err := env.AllInstances() + if err != nil { + return nil, err + } + + for i, id := range ids { + for _, instance := range allInstances { + if instance.Id() == id { + instances[i] = instance + found++ + } + } + } + + logger.Debugf("Found %d instances %q", found, instances) + + if found == 0 { + return nil, environs.ErrNoInstances + } else if found < len(ids) { + return instances, environs.ErrPartialInstances + } + + return instances, nil +} + +func (env *joyentEnviron) StopInstances(instances []instance.Instance) error { + // Remove all the instances in parallel so that we incur less round-trips. + var wg sync.WaitGroup + //var err error + wg.Add(len(instances)) + errc := make(chan error, len(instances)) + for _, inst := range instances { + inst := inst.(*joyentInstance) + go func() { + defer wg.Done() + if err := inst.Stop(); err != nil { + errc <- err + } + }() + } + wg.Wait() + select { + case err := <-errc: + return fmt.Errorf("cannot stop all instances: %v", err) + default: + } + + return nil +} + +// findInstanceSpec returns an InstanceSpec satisfying the supplied instanceConstraint. +func (env *joyentEnviron) FindInstanceSpec(ic *instances.InstanceConstraint) (*instances.InstanceSpec, error) { + packages, err := env.compute.cloudapi.ListPackages(nil) + if err != nil { + return nil, err + } + allInstanceTypes := []instances.InstanceType{} + for _, pkg := range packages { + instanceType := instances.InstanceType{ + Id: pkg.Id, + Name: pkg.Name, + Arches: ic.Arches, + Mem: uint64(pkg.Memory), + CpuCores: uint64(pkg.VCPUs), + RootDisk: uint64(pkg.Disk * 1024), + VirtType: &vTypeVirtualmachine, + } + allInstanceTypes = append(allInstanceTypes, instanceType) + } + + imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{ + CloudSpec: simplestreams.CloudSpec{ic.Region, env.Ecfg().SdcUrl()}, + Series: []string{ic.Series}, + Arches: ic.Arches, + }) + sources, err := imagemetadata.GetMetadataSources(env) + if err != nil { + return nil, err + } + + matchingImages, _, err := imagemetadata.Fetch(sources, simplestreams.DefaultIndexPath, imageConstraint, signedImageDataOnly) + if err != nil { + return nil, err + } + images := instances.ImageMetadataToImages(matchingImages) + spec, err := instances.FindInstanceSpec(images, ic, allInstanceTypes) + if err != nil { + return nil, err + } + return spec, nil } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/export_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/export_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/export_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/export_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -4,7 +4,195 @@ package joyent import ( + "bytes" + "fmt" + "text/template" + + "github.com/joyent/gosign/auth" + gc "launchpad.net/gocheck" + + "launchpad.net/juju-core/constraints" "launchpad.net/juju-core/environs" + "launchpad.net/juju-core/environs/config" + "launchpad.net/juju-core/environs/configstore" + "launchpad.net/juju-core/environs/imagemetadata" + "launchpad.net/juju-core/environs/instances" + "launchpad.net/juju-core/environs/jujutest" + "launchpad.net/juju-core/environs/storage" + "launchpad.net/juju-core/testing" ) -var Provider environs.EnvironProvider = providerInstance +var Provider environs.EnvironProvider = GetProviderInstance() +var EnvironmentVariables = environmentVariables + +var indexData = ` + { + "index": { + "com.ubuntu.cloud:released:joyent": { + "updated": "Fri, 14 Feb 2014 13:39:35 +0000", + "clouds": [ + { + "region": "{{.Region}}", + "endpoint": "{{.SdcEndpoint.URL}}" + } + ], + "cloudname": "joyent", + "datatype": "image-ids", + "format": "products:1.0", + "products": [ + "com.ubuntu.cloud:server:12.04:amd64", + "com.ubuntu.cloud:server:12.10:amd64", + "com.ubuntu.cloud:server:13.04:amd64" + ], + "path": "streams/v1/com.ubuntu.cloud:released:joyent.json" + } + }, + "updated": "Fri, 14 Feb 2014 13:39:35 +0000", + "format": "index:1.0" + } +` + +var imagesData = ` +{ + "content_id": "com.ubuntu.cloud:released:joyent", + "format": "products:1.0", + "updated": "Fri, 14 Feb 2014 13:39:35 +0000", + "datatype": "image-ids", + "products": { + "com.ubuntu.cloud:server:12.04:amd64": { + "release": "precise", + "version": "12.04", + "arch": "amd64", + "versions": { + "20140214": { + "items": { + "11223344-0a0a-ff99-11bb-0a1b2c3d4e5f": { + "region": "some-region", + "id": "11223344-0a0a-ff99-11bb-0a1b2c3d4e5f", + "virt": "kvm" + } + }, + "pubname": "ubuntu-precise-12.04-amd64-server-20140214", + "label": "release" + } + } + }, + "com.ubuntu.cloud:server:12.10:amd64": { + "release": "quantal", + "version": "12.10", + "arch": "amd64", + "versions": { + "20140214": { + "items": { + "11223344-0a0a-ee88-22ab-00aa11bb22cc": { + "region": "some-region", + "id": "11223344-0a0a-ee88-22ab-00aa11bb22cc", + "virt": "kvm" + } + }, + "pubname": "ubuntu-quantal-12.10-amd64-server-20140214", + "label": "release" + } + } + }, + "com.ubuntu.cloud:server:13.04:amd64": { + "release": "raring", + "version": "13.04", + "arch": "amd64", + "versions": { + "20140214": { + "items": { + "11223344-0a0a-dd77-33cd-abcd1234e5f6": { + "region": "some-region", + "id": "11223344-0a0a-dd77-33cd-abcd1234e5f6", + "virt": "kvm" + } + }, + "pubname": "ubuntu-raring-13.04-amd64-server-20140214", + "label": "release" + } + } + } + } +} +` + +func parseIndexData(creds *auth.Credentials) bytes.Buffer { + var metadata bytes.Buffer + + t := template.Must(template.New("").Parse(indexData)) + if err := t.Execute(&metadata, creds); err != nil { + panic(fmt.Errorf("cannot generate index metdata: %v", err)) + } + + return metadata +} + +// This provides the content for code accessing test://host/... URLs. This allows +// us to set the responses for things like the Metadata server, by pointing +// metadata requests at test://host/... +var testRoundTripper = &jujutest.ProxyRoundTripper{} + +func init() { + testRoundTripper.RegisterForScheme("test") +} + +var origImagesUrl = imagemetadata.DefaultBaseURL + +// Set Metadata requests to be served by the filecontent supplied. +func UseExternalTestImageMetadata(creds *auth.Credentials) { + metadata := parseIndexData(creds) + files := map[string]string{ + "/streams/v1/index.json": metadata.String(), + "/streams/v1/com.ubuntu.cloud:released:joyent.json": imagesData, + } + testRoundTripper.Sub = jujutest.NewCannedRoundTripper(files, nil) + imagemetadata.DefaultBaseURL = "test://host" +} + +func UnregisterExternalTestImageMetadata() { + testRoundTripper.Sub = nil +} + +func FindInstanceSpec(e environs.Environ, series, arch, cons string) (spec *instances.InstanceSpec, err error) { + env := e.(*joyentEnviron) + spec, err = env.FindInstanceSpec(&instances.InstanceConstraint{ + Series: series, + Arches: []string{arch}, + Region: env.Ecfg().Region(), + Constraints: constraints.MustParse(cons), + }) + return +} + +func ControlBucketName(e environs.Environ) string { + env := e.(*joyentEnviron) + return env.Storage().(*JoyentStorage).GetContainerName() +} + +func CreateContainer(s *JoyentStorage) error { + return s.createContainer() +} + +// MakeConfig creates a functional environConfig for a test. +func MakeConfig(c *gc.C, attrs testing.Attrs) *environConfig { + cfg, err := config.New(config.NoDefaults, attrs) + c.Assert(err, gc.IsNil) + env, err := environs.Prepare(cfg, testing.Context(c), configstore.NewMem()) + c.Assert(err, gc.IsNil) + return env.(*joyentEnviron).Ecfg() +} + +// MakeCredentials creates credentials for a test. +func MakeCredentials(c *gc.C, attrs testing.Attrs) *auth.Credentials { + creds, err := credentials(MakeConfig(c, attrs)) + c.Assert(err, gc.IsNil) + return creds +} + +// MakeStorage creates an env storage for a test. +func MakeStorage(c *gc.C, attrs testing.Attrs) storage.Storage { + stor, err := newStorage(MakeConfig(c, attrs), "") + c.Assert(err, gc.IsNil) + return stor +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/instance_firewall.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/instance_firewall.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/instance_firewall.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/instance_firewall.go 2014-04-04 16:53:35.000000000 +0000 @@ -4,23 +4,102 @@ package joyent import ( + "fmt" + "strings" + + "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/instance" + + "github.com/joyent/gosdc/cloudapi" +) + +const ( + firewallRuleVm = "FROM tag %s TO vm %s ALLOW %s PORT %d" ) -// Implementing the methods below (to do something other than return nil) will -// cause `juju expose` to work when the firewall-mode is "instance". If you -// implement one of them, you should implement them all. +// Helper method to create a firewall rule string for the given machine Id and port +func createFirewallRuleVm(env *joyentEnviron, machineId string, port instance.Port) string { + return fmt.Sprintf(firewallRuleVm, env.Name(), machineId, strings.ToLower(port.Protocol), port.Number) +} + +func (inst *joyentInstance) OpenPorts(machineId string, ports []instance.Port) error { + if inst.env.Config().FirewallMode() != config.FwInstance { + return fmt.Errorf("invalid firewall mode %q for opening ports on instance", inst.env.Config().FirewallMode()) + } + + fwRules, err := inst.env.compute.cloudapi.ListFirewallRules() + if err != nil { + return fmt.Errorf("cannot get firewall rules: %v", err) + } + + machineId = string(inst.Id()) + for _, p := range ports { + rule := createFirewallRuleVm(inst.env, machineId, p) + if e, id := ruleExists(fwRules, rule); e { + _, err := inst.env.compute.cloudapi.EnableFirewallRule(id) + if err != nil { + return fmt.Errorf("couldn't enable rule %s: %v", rule, err) + } + } else { + _, err := inst.env.compute.cloudapi.CreateFirewallRule(cloudapi.CreateFwRuleOpts{ + Enabled: true, + Rule: rule, + }) + if err != nil { + return fmt.Errorf("couldn't create rule %s: %v", rule, err) + } + } + } + + logger.Infof("ports %v opened for instance %q", ports, machineId) -func (inst *environInstance) OpenPorts(machineId string, ports []instance.Port) error { - logger.Warningf("pretending to open ports %v for instance %q", ports, inst.id) return nil } -func (inst *environInstance) ClosePorts(machineId string, ports []instance.Port) error { - logger.Warningf("pretending to close ports %v for instance %q", ports, inst.id) +func (inst *joyentInstance) ClosePorts(machineId string, ports []instance.Port) error { + if inst.env.Config().FirewallMode() != config.FwInstance { + return fmt.Errorf("invalid firewall mode %q for closing ports on instance", inst.env.Config().FirewallMode()) + } + + fwRules, err := inst.env.compute.cloudapi.ListFirewallRules() + if err != nil { + return fmt.Errorf("cannot get firewall rules: %v", err) + } + + machineId = string(inst.Id()) + for _, p := range ports { + rule := createFirewallRuleVm(inst.env, machineId, p) + if e, id := ruleExists(fwRules, rule); e { + _, err := inst.env.compute.cloudapi.DisableFirewallRule(id) + if err != nil { + return fmt.Errorf("couldn't disable rule %s: %v", rule, err) + } + } else { + _, err := inst.env.compute.cloudapi.CreateFirewallRule(cloudapi.CreateFwRuleOpts{ + Enabled: false, + Rule: rule, + }) + if err != nil { + return fmt.Errorf("couldn't create rule %s: %v", rule, err) + } + } + } + + logger.Infof("ports %v closed for instance %q", ports, machineId) + return nil } -func (inst *environInstance) Ports(machineId string) ([]instance.Port, error) { - return nil, nil +func (inst *joyentInstance) Ports(machineId string) ([]instance.Port, error) { + if inst.env.Config().FirewallMode() != config.FwInstance { + return nil, fmt.Errorf("invalid firewall mode %q for retrieving ports from instance", inst.env.Config().FirewallMode()) + } + + machineId = string(inst.Id()) + fwRules, err := inst.env.compute.cloudapi.ListMachineFirewallRules(machineId) + if err != nil { + return nil, fmt.Errorf("cannot get firewall rules: %v", err) + } + + return getPorts(inst.env, fwRules), nil } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/instance.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/instance.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/instance.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/instance.go 2014-04-04 16:57:31.000000000 +0000 @@ -4,46 +4,99 @@ package joyent import ( + "fmt" + "strings" + "time" + + "github.com/joyent/gosdc/cloudapi" + "launchpad.net/juju-core/instance" "launchpad.net/juju-core/provider/common" ) -type environInstance struct { - id instance.Id - env *environ +type joyentInstance struct { + machine *cloudapi.Machine + env *joyentEnviron } -var _ instance.Instance = (*environInstance)(nil) +var _ instance.Instance = (*joyentInstance)(nil) -func (inst *environInstance) Id() instance.Id { - return inst.id +func (inst *joyentInstance) Id() instance.Id { + return instance.Id(inst.machine.Id) } -func (inst *environInstance) Status() string { - _ = inst.env.getSnapshot() - return "unknown (not implemented)" +func (inst *joyentInstance) Status() string { + return inst.machine.State } -func (inst *environInstance) Refresh() error { +func (inst *joyentInstance) Refresh() error { return nil } -func (inst *environInstance) Addresses() ([]instance.Address, error) { - _ = inst.env.getSnapshot() - return nil, errNotImplemented -} - -func (inst *environInstance) DNSName() (string, error) { - // This method is likely to be replaced entirely by Addresses() at some point, - // but remains necessary for now. It's probably smart to implement it in - // terms of Addresses above, to minimise churn when it's removed. - _ = inst.env.getSnapshot() - return "", errNotImplemented -} - -func (inst *environInstance) WaitDNSName() (string, error) { - // This method is likely to be replaced entirely by Addresses() at some point, - // but remains necessary for now. Until it's finally removed, you can probably - // ignore this method; the common implementation should work. +func (inst *joyentInstance) Addresses() ([]instance.Address, error) { + addresses := make([]instance.Address, len(inst.machine.IPs)) + for _, ip := range inst.machine.IPs { + address := instance.NewAddress(ip) + if ip == inst.machine.PrimaryIP { + address.NetworkScope = instance.NetworkPublic + } else { + address.NetworkScope = instance.NetworkCloudLocal + } + addresses = append(addresses, address) + } + + return addresses, nil +} + +func (inst *joyentInstance) DNSName() (string, error) { + addresses, err := inst.Addresses() + if err != nil { + return "", err + } + addr := instance.SelectPublicAddress(addresses) + if addr == "" { + return "", instance.ErrNoDNSName + } + return addr, nil +} + +func (inst *joyentInstance) WaitDNSName() (string, error) { return common.WaitDNSName(inst) } + +// Stop will stop and delete the machine +// Stopped machines are still billed for in the Joyent Public Cloud +func (inst *joyentInstance) Stop() error { + id := string(inst.Id()) + + // wait for machine to be running + // if machine is still provisioning stop will fail + for !inst.pollMachineState(id, "running") { + time.Sleep(1 * time.Second) + } + + err := inst.env.compute.cloudapi.StopMachine(id) + if err != nil { + return fmt.Errorf("cannot stop instance %s: %v", id, err) + } + + // wait for machine to be stopped + for !inst.pollMachineState(id, "stopped") { + time.Sleep(1 * time.Second) + } + + err = inst.env.compute.cloudapi.DeleteMachine(id) + if err != nil { + return fmt.Errorf("cannot delete instance %s: %v", id, err) + } + + return nil +} + +func (inst *joyentInstance) pollMachineState(machineId, state string) bool { + machineConfig, err := inst.env.compute.cloudapi.GetMachine(machineId) + if err != nil { + return false + } + return strings.EqualFold(machineConfig.State, state) +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/joyent_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/joyent_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/joyent_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/joyent_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -4,24 +4,105 @@ package joyent_test import ( - "testing" + "fmt" + "io/ioutil" + "os" gc "launchpad.net/gocheck" - "launchpad.net/juju-core/environs" - "launchpad.net/juju-core/provider/joyent" + envtesting "launchpad.net/juju-core/environs/testing" + coretesting "launchpad.net/juju-core/testing" + "launchpad.net/juju-core/utils" + "launchpad.net/juju-core/version" ) -func TestPackage(t *testing.T) { - gc.TestingT(t) +const ( + testUser = "test" + testKeyFileName = "provider_id_rsa" + testPrivateKey = `-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAza+KvczCrcpQGRq9e347VHx9oEvuhseJt0ydR+UMAveyQprU +4JHvzwUUhGnG147GJQYyfQ4nzaSG62az/YThoZJzw8gtxGkVHv0wlAlRkYhxbKbq +8WQIh73xDQkHLw2lXLvf7Tt0Mhow0qGEmkOjTb5fPsj2evphrV3jJ15QlhL4cv33 +t8jVadIrL0iIpwdqWiPqUKpsrSfKJghkoXS6quPy78P820TnuoBG+/Ppr8Kkvn6m +A7j4xnOQ12QE6jPK4zkikj5ZczSC4fTG0d3BwwX4VYu+4y/T/BX0L9VNUmQU22Y+ +/MRXAUZxsa8VhNB+xXF5XSubyb2n6loMWWaYGwIDAQABAoIBAQDCJt9JxYxGS+BL +sigF9+O9Hj3fH42p/5QJR/J2uMgbzP+hS1GCIX9B5MO3MbmWI5j5vd3OmZwMyy7n +6Wwg9FufDgTkW4KIEcD0HX7LXfh27VpTe0PuU8SRjUOKUGlNiw36eQUog6Rs3rgT +Oo9Wpl3xtq9lLoErGEk3QpZ2xNpArTfsN9N3pdmD4sv7wmJq0PZQyej482g9R0g/ +5k2ni6JpcEifzBQ6Bzx3EV2l9UipEIqbqDpMOtYFCpnLQhEaDfUribqXINGIsjiq +VyFa3Mbg/eayqG3UX3rVTCif2NnW2ojl4mMgWCyxgWfb4Jg1trc3v7X4SXfbgPWD +WcfrOhOhAoGBAP7ZC8KHAnjujwvXf3PxVNm6CTs5evbByQVyxNciGxRuOomJIR4D +euqepQ4PuNAabnrbMyQWXpibIKpmLnBVoj1q0IMXYvi2MZF5e2tH/Gx01UvxamHh +bKhHmp9ImHhVl6kObXOdNvLVTt/BI5FZBblvm7qOoiVwImPbqqVHP7Q5AoGBAM6d +mNsrW0iV/nP1m7d8mcFw74PI0FNlNdfUoePUgokO0t5OU0Ri/lPBDCRGlvVF3pj1 +HnmwroNtdWr9oPVB6km8193fb2zIWe53tj+6yRFQpz5elrSPfeZaZXlJZAGCCCdN +gBggWQFPeQiT54aPywPpcTZHIs72XBqQ6QsIPrbzAoGAdW2hg5MeSobyFuzHZ69N +/70/P7DuvgDxFbeah97JR5K7GmC7h87mtnE/cMlByXJEcgvK9tfv4rWoSZwnzc9H +oLE1PxJpolyhXnzxp69V2svC9OlasZtjq+7Cip6y0s/twBJL0Lgid6ZeX6/pKbIx +dw68XSwX/tQ6pHS1ns7DxdECgYBJbBWapNCefbbbjEcWsC+PX0uuABmP2SKGHSie +ZrEwdVUX7KuIXMlWB/8BkRgp9vdAUbLPuap6R9Z2+8RMA213YKUxUiotdREIPgBE +q2KyRX/5GPHjHi62Qh9XN25TXtr45ICFklEutwgitTSMS+Lv8+/oQuUquL9ILYCz +C+4FYwKBgQDE9yZTUpJjG2424z6bl/MHzwl5RB4pMronp0BbeVqPwhCBfj0W5I42 +1ZL4+8eniHfUs4GXzf5tb9YwVt3EltIF2JybaBvFsv2o356yJUQmqQ+jyYRoEpT5 +2SwilFo/XCotCXxi5n8sm9V94a0oix4ehZrohTA/FZLsggwFCPmXfw== +-----END RSA PRIVATE KEY-----` + testKeyFingerprint = "66:ca:1c:09:75:99:35:69:be:91:08:25:03:c0:17:c0" +) + +type providerSuite struct { + coretesting.FakeHomeSuite + envtesting.ToolsFixture + restoreTimeouts func() +} + +var _ = gc.Suite(&providerSuite{}) + +func (s *providerSuite) SetUpSuite(c *gc.C) { + s.restoreTimeouts = envtesting.PatchAttemptStrategies() + s.FakeHomeSuite.SetUpSuite(c) + s.AddSuiteCleanup(CreateTestKey(c)) +} + +func (s *providerSuite) TearDownSuite(c *gc.C) { + s.restoreTimeouts() + s.FakeHomeSuite.TearDownSuite(c) +} + +func (s *providerSuite) SetUpTest(c *gc.C) { + s.FakeHomeSuite.SetUpTest(c) + s.ToolsFixture.SetUpTest(c) + s.AddCleanup(CreateTestKey(c)) } -type JoyentSuite struct{} +func (s *providerSuite) TearDownTest(c *gc.C) { + s.ToolsFixture.TearDownTest(c) + s.LoggingSuite.TearDownTest(c) +} -var _ = gc.Suite(&JoyentSuite{}) +func GetFakeConfig(sdcUrl, mantaUrl string) coretesting.Attrs { + return coretesting.FakeConfig().Merge(coretesting.Attrs{ + "name": "joyent test environment", + "type": "joyent", + "sdc-user": testUser, + "sdc-key-id": testKeyFingerprint, + "sdc-url": sdcUrl, + "manta-user": testUser, + "manta-key-id": testKeyFingerprint, + "manta-url": mantaUrl, + "private-key-path": fmt.Sprintf("~/.ssh/%s", testKeyFileName), + "algorithm": "rsa-sha256", + "control-dir": "juju-test", + "agent-version": version.Current.Number.String(), + }) +} -func (*JoyentSuite) TestRegistered(c *gc.C) { - provider, err := environs.Provider("joyent") +func CreateTestKey(c *gc.C) func(*gc.C) { + keyFile := fmt.Sprintf("~/.ssh/%s", testKeyFileName) + keyFilePath, err := utils.NormalizePath(keyFile) + c.Assert(err, gc.IsNil) + err = ioutil.WriteFile(keyFilePath, []byte(testPrivateKey), 400) c.Assert(err, gc.IsNil) - c.Assert(provider, gc.Equals, joyent.Provider) + return func(c *gc.C) { + os.Remove(keyFilePath) + } } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/live_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/live_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/live_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/live_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -0,0 +1,94 @@ +// Copyright 2013 Joyent Inc. +// Licensed under the AGPLv3, see LICENCE file for details. + +package joyent_test + +import ( + "crypto/rand" + "fmt" + "io" + "os" + + gc "launchpad.net/gocheck" + + "launchpad.net/juju-core/environs/config" + "launchpad.net/juju-core/environs/jujutest" + envtesting "launchpad.net/juju-core/environs/testing" + coretesting "launchpad.net/juju-core/testing" + "launchpad.net/juju-core/testing/testbase" + "launchpad.net/juju-core/version" +) + +// uniqueName is generated afresh for every test run, so that +// we are not polluted by previous test state. +var uniqueName = randomName() + +func randomName() string { + buf := make([]byte, 8) + _, err := io.ReadFull(rand.Reader, buf) + if err != nil { + panic(fmt.Sprintf("error from crypto rand: %v", err)) + } + return fmt.Sprintf("%x", buf) +} + +func registerLiveTests() { + attrs := coretesting.FakeConfig().Merge(map[string]interface{}{ + "name": "sample-" + uniqueName, + "type": "joyent", + "sdc-user": os.Getenv("SDC_ACCOUNT"), + "sdc-key-id": os.Getenv("SDC_KEY_ID"), + "manta-user": os.Getenv("MANTA_USER"), + "manta-key-id": os.Getenv("MANTA_KEY_ID"), + "control-dir": "juju-test-" + uniqueName, + "admin-secret": "for real", + "firewall-mode": config.FwInstance, + "agent-version": version.Current.Number.String(), + }) + gc.Suite(&LiveTests{ + LiveTests: jujutest.LiveTests{ + TestConfig: attrs, + CanOpenState: true, + HasProvisioner: true, + }, + }) +} + +// LiveTests contains tests that can be run against the Joyent Public Cloud. +type LiveTests struct { + testbase.LoggingSuite + jujutest.LiveTests +} + +func (t *LiveTests) SetUpSuite(c *gc.C) { + t.LoggingSuite.SetUpSuite(c) + + t.LiveTests.SetUpSuite(c) + // For testing, we create a storage instance to which is uploaded tools and image metadata. + t.PrepareOnce(c) + c.Assert(t.Env.Storage(), gc.NotNil) + // Put some fake tools metadata in place so that tests that are simply + // starting instances without any need to check if those instances + // are running can find the metadata. + envtesting.UploadFakeTools(c, t.Env.Storage()) +} + +func (t *LiveTests) TearDownSuite(c *gc.C) { + if t.Env == nil { + // This can happen if SetUpSuite fails. + return + } + t.LiveTests.TearDownSuite(c) + t.LoggingSuite.TearDownSuite(c) +} + +func (t *LiveTests) SetUpTest(c *gc.C) { + t.LoggingSuite.SetUpTest(c) + t.LiveTests.SetUpTest(c) + c.Assert(t.Env.Storage(), gc.NotNil) +} + +func (t *LiveTests) TearDownTest(c *gc.C) { + t.LiveTests.TearDownTest(c) + t.LoggingSuite.TearDownTest(c) +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/local_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/local_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/local_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/local_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -0,0 +1,423 @@ +// Copyright 2013 Joyent Inc. +// Licensed under the AGPLv3, see LICENCE file for details. + +package joyent_test + +import ( + "bytes" + "io/ioutil" + "net/http" + "net/http/httptest" + "strings" + + lm "github.com/joyent/gomanta/localservices/manta" + lc "github.com/joyent/gosdc/localservices/cloudapi" + jc "github.com/juju/testing/checkers" + gc "launchpad.net/gocheck" + + "launchpad.net/juju-core/constraints" + "launchpad.net/juju-core/environs" + "launchpad.net/juju-core/environs/bootstrap" + "launchpad.net/juju-core/environs/imagemetadata" + "launchpad.net/juju-core/environs/jujutest" + "launchpad.net/juju-core/environs/simplestreams" + "launchpad.net/juju-core/environs/storage" + envtesting "launchpad.net/juju-core/environs/testing" + "launchpad.net/juju-core/environs/tools" + "launchpad.net/juju-core/instance" + "launchpad.net/juju-core/juju/testing" + "launchpad.net/juju-core/provider/joyent" + coretesting "launchpad.net/juju-core/testing" + "launchpad.net/juju-core/testing/testbase" +) + +type ProviderSuite struct{} + +var _ = gc.Suite(&ProviderSuite{}) + +func registerLocalTests() { + gc.Suite(&localServerSuite{}) + gc.Suite(&localLiveSuite{}) +} + +type localCloudAPIServer struct { + Server *httptest.Server + Mux *http.ServeMux + oldHandler http.Handler + cloudapi *lc.CloudAPI +} + +func (ca *localCloudAPIServer) setupServer(c *gc.C) { + // Set up the HTTP server. + ca.Server = httptest.NewServer(nil) + c.Assert(ca.Server, gc.NotNil) + ca.oldHandler = ca.Server.Config.Handler + ca.Mux = http.NewServeMux() + ca.Server.Config.Handler = ca.Mux + + ca.cloudapi = lc.New(ca.Server.URL, testUser) + ca.cloudapi.SetupHTTP(ca.Mux) + c.Logf("Started local CloudAPI service at: %v", ca.Server.URL) +} + +func (c *localCloudAPIServer) destroyServer() { + c.Mux = nil + c.Server.Config.Handler = c.oldHandler + c.Server.Close() +} + +type localMantaServer struct { + Server *httptest.Server + Mux *http.ServeMux + oldHandler http.Handler + manta *lm.Manta +} + +func (m *localMantaServer) setupServer(c *gc.C) { + // Set up the HTTP server. + m.Server = httptest.NewServer(nil) + c.Assert(m.Server, gc.NotNil) + m.oldHandler = m.Server.Config.Handler + m.Mux = http.NewServeMux() + m.Server.Config.Handler = m.Mux + + m.manta = lm.New(m.Server.URL, testUser) + m.manta.SetupHTTP(m.Mux) + c.Logf("Started local Manta service at: %v", m.Server.URL) +} + +func (m *localMantaServer) destroyServer() { + m.Mux = nil + m.Server.Config.Handler = m.oldHandler + m.Server.Close() +} + +type localLiveSuite struct { + testbase.LoggingSuite + LiveTests + cSrv *localCloudAPIServer + mSrv *localMantaServer +} + +func (s *localLiveSuite) SetUpSuite(c *gc.C) { + s.LoggingSuite.SetUpSuite(c) + s.AddSuiteCleanup(CreateTestKey(c)) + s.cSrv = &localCloudAPIServer{} + s.mSrv = &localMantaServer{} + s.cSrv.setupServer(c) + s.mSrv.setupServer(c) + + s.TestConfig = GetFakeConfig(s.cSrv.Server.URL, s.mSrv.Server.URL) + s.TestConfig = s.TestConfig.Merge(coretesting.Attrs{ + "image-metadata-url": "test://host", + }) + s.LiveTests.SetUpSuite(c) + + creds := joyent.MakeCredentials(c, s.TestConfig) + joyent.UseExternalTestImageMetadata(creds) + restoreFinishBootstrap := envtesting.DisableFinishBootstrap() + s.AddSuiteCleanup(func(*gc.C) { restoreFinishBootstrap() }) +} + +func (s *localLiveSuite) TearDownSuite(c *gc.C) { + joyent.UnregisterExternalTestImageMetadata() + s.LiveTests.TearDownSuite(c) + s.cSrv.destroyServer() + s.mSrv.destroyServer() + s.LoggingSuite.TearDownSuite(c) +} + +func (s *localLiveSuite) SetUpTest(c *gc.C) { + s.LoggingSuite.SetUpTest(c) + s.LiveTests.SetUpTest(c) +} + +func (s *localLiveSuite) TearDownTest(c *gc.C) { + s.LiveTests.TearDownTest(c) + s.LoggingSuite.TearDownTest(c) +} + +// localServerSuite contains tests that run against an Joyent service double. +// These tests can test things that would be unreasonably slow or expensive +// to test on a live Joyent server. The service double is started and stopped for +// each test. +type localServerSuite struct { + jujutest.Tests + cSrv *localCloudAPIServer + mSrv *localMantaServer +} + +func (s *localServerSuite) SetUpSuite(c *gc.C) { + s.Tests.SetUpSuite(c) + restoreFinishBootstrap := envtesting.DisableFinishBootstrap() + s.AddSuiteCleanup(func(*gc.C) { restoreFinishBootstrap() }) +} + +func (s *localServerSuite) TearDownSuite(c *gc.C) { + s.Tests.TearDownSuite(c) +} + +func (s *localServerSuite) SetUpTest(c *gc.C) { + s.AddSuiteCleanup(CreateTestKey(c)) + + s.cSrv = &localCloudAPIServer{} + s.mSrv = &localMantaServer{} + s.cSrv.setupServer(c) + s.mSrv.setupServer(c) + + s.Tests.SetUpTest(c) + s.TestConfig = GetFakeConfig(s.cSrv.Server.URL, s.mSrv.Server.URL) + + // Put some fake image metadata in place. + creds := joyent.MakeCredentials(c, s.TestConfig) + joyent.UseExternalTestImageMetadata(creds) +} + +func (s *localServerSuite) TearDownTest(c *gc.C) { + joyent.UnregisterExternalTestImageMetadata() + s.Tests.TearDownTest(c) + s.cSrv.destroyServer() + s.mSrv.destroyServer() +} + +func bootstrapContext(c *gc.C) environs.BootstrapContext { + return coretesting.Context(c) +} + +// If the environment is configured not to require a public IP address for nodes, +// bootstrapping and starting an instance should occur without any attempt to +// allocate a public address. +func (s *localServerSuite) TestStartInstance(c *gc.C) { + env := s.Prepare(c) + envtesting.UploadFakeTools(c, env.Storage()) + err := bootstrap.Bootstrap(bootstrapContext(c), env, constraints.Value{}) + c.Assert(err, gc.IsNil) + inst, _ := testing.AssertStartInstance(c, env, "100") + err = env.StopInstances([]instance.Instance{inst}) + c.Assert(err, gc.IsNil) +} + +func (s *localServerSuite) TestStartInstanceHardwareCharacteristics(c *gc.C) { + env := s.Prepare(c) + envtesting.UploadFakeTools(c, env.Storage()) + err := bootstrap.Bootstrap(bootstrapContext(c), env, constraints.Value{}) + c.Assert(err, gc.IsNil) + _, hc := testing.AssertStartInstanceWithConstraints(c, env, "100", constraints.MustParse("mem=1024")) + c.Check(*hc.Arch, gc.Equals, "amd64") + c.Check(*hc.Mem, gc.Equals, uint64(1024)) + c.Check(*hc.CpuCores, gc.Equals, uint64(1)) + c.Assert(hc.CpuPower, gc.IsNil) +} + +var instanceGathering = []struct { + ids []instance.Id + err error +}{ + {ids: []instance.Id{"id0"}}, + {ids: []instance.Id{"id0", "id0"}}, + {ids: []instance.Id{"id0", "id1"}}, + {ids: []instance.Id{"id1", "id0"}}, + {ids: []instance.Id{"id1", "id0", "id1"}}, + { + ids: []instance.Id{""}, + err: environs.ErrNoInstances, + }, + { + ids: []instance.Id{"", ""}, + err: environs.ErrNoInstances, + }, + { + ids: []instance.Id{"", "", ""}, + err: environs.ErrNoInstances, + }, + { + ids: []instance.Id{"id0", ""}, + err: environs.ErrPartialInstances, + }, + { + ids: []instance.Id{"", "id1"}, + err: environs.ErrPartialInstances, + }, + { + ids: []instance.Id{"id0", "id1", ""}, + err: environs.ErrPartialInstances, + }, + { + ids: []instance.Id{"id0", "", "id0"}, + err: environs.ErrPartialInstances, + }, + { + ids: []instance.Id{"id0", "id0", ""}, + err: environs.ErrPartialInstances, + }, + { + ids: []instance.Id{"", "id0", "id1"}, + err: environs.ErrPartialInstances, + }, +} + +func (s *localServerSuite) TestInstanceStatus(c *gc.C) { + env := s.Prepare(c) + envtesting.UploadFakeTools(c, env.Storage()) + inst, _ := testing.AssertStartInstance(c, env, "100") + c.Assert(inst.Status(), gc.Equals, "running") + err := env.StopInstances([]instance.Instance{inst}) + c.Assert(err, gc.IsNil) +} + +func (s *localServerSuite) TestInstancesGathering(c *gc.C) { + env := s.Prepare(c) + envtesting.UploadFakeTools(c, env.Storage()) + inst0, _ := testing.AssertStartInstance(c, env, "100") + id0 := inst0.Id() + inst1, _ := testing.AssertStartInstance(c, env, "101") + id1 := inst1.Id() + c.Logf("id0: %s, id1: %s", id0, id1) + defer func() { + err := env.StopInstances([]instance.Instance{inst0, inst1}) + c.Assert(err, gc.IsNil) + }() + + for i, test := range instanceGathering { + c.Logf("test %d: find %v -> expect len %d, err: %v", i, test.ids, len(test.ids), test.err) + ids := make([]instance.Id, len(test.ids)) + for j, id := range test.ids { + switch id { + case "id0": + ids[j] = id0 + case "id1": + ids[j] = id1 + } + } + insts, err := env.Instances(ids) + c.Assert(err, gc.Equals, test.err) + if err == environs.ErrNoInstances { + c.Assert(insts, gc.HasLen, 0) + } else { + c.Assert(insts, gc.HasLen, len(test.ids)) + } + for j, inst := range insts { + if ids[j] != "" { + c.Assert(inst.Id(), gc.Equals, ids[j]) + } else { + c.Assert(inst, gc.IsNil) + } + } + } +} + +// It should be moved to environs.jujutests.Tests. +func (s *localServerSuite) TestBootstrapInstanceUserDataAndState(c *gc.C) { + env := s.Prepare(c) + envtesting.UploadFakeTools(c, env.Storage()) + err := bootstrap.Bootstrap(bootstrapContext(c), env, constraints.Value{}) + c.Assert(err, gc.IsNil) + + // check that the state holds the id of the bootstrap machine. + stateData, err := bootstrap.LoadState(env.Storage()) + c.Assert(err, gc.IsNil) + c.Assert(stateData.StateInstances, gc.HasLen, 1) + + insts, err := env.AllInstances() + c.Assert(err, gc.IsNil) + c.Assert(insts, gc.HasLen, 1) + c.Check(stateData.StateInstances[0], gc.Equals, insts[0].Id()) + + bootstrapDNS, err := insts[0].DNSName() + c.Assert(err, gc.IsNil) + c.Assert(bootstrapDNS, gc.Not(gc.Equals), "") +} + +func (s *localServerSuite) TestGetImageMetadataSources(c *gc.C) { + env := s.Prepare(c) + sources, err := imagemetadata.GetMetadataSources(env) + c.Assert(err, gc.IsNil) + c.Assert(len(sources), gc.Equals, 2) + var urls = make([]string, len(sources)) + for i, source := range sources { + url, err := source.URL("") + c.Assert(err, gc.IsNil) + urls[i] = url + } + // The control bucket URL contains the bucket name. + c.Assert(strings.Contains(urls[0], joyent.ControlBucketName(env)+"/images"), jc.IsTrue) +} + +func (s *localServerSuite) TestGetToolsMetadataSources(c *gc.C) { + env := s.Prepare(c) + sources, err := tools.GetMetadataSources(env) + c.Assert(err, gc.IsNil) + c.Assert(len(sources), gc.Equals, 1) + var urls = make([]string, len(sources)) + for i, source := range sources { + url, err := source.URL("") + c.Assert(err, gc.IsNil) + urls[i] = url + } + // The control bucket URL contains the bucket name. + c.Assert(strings.Contains(urls[0], joyent.ControlBucketName(env)+"/tools"), jc.IsTrue) +} + +func (s *localServerSuite) TestFindImageBadDefaultImage(c *gc.C) { + env := s.Prepare(c) + // An error occurs if no suitable image is found. + _, err := joyent.FindInstanceSpec(env, "saucy", "amd64", "mem=4G") + c.Assert(err, gc.ErrorMatches, `no "saucy" images in some-region with arches \[amd64\]`) +} + +func (s *localServerSuite) TestValidateImageMetadata(c *gc.C) { + env := s.Prepare(c) + params, err := env.(simplestreams.MetadataValidator).MetadataLookupParams("some-region") + c.Assert(err, gc.IsNil) + params.Sources, err = imagemetadata.GetMetadataSources(env) + c.Assert(err, gc.IsNil) + params.Series = "raring" + image_ids, _, err := imagemetadata.ValidateImageMetadata(params) + c.Assert(err, gc.IsNil) + c.Assert(image_ids, gc.DeepEquals, []string{"11223344-0a0a-dd77-33cd-abcd1234e5f6"}) +} + +func (s *localServerSuite) TestRemoveAll(c *gc.C) { + env := s.Prepare(c) + stor := env.Storage() + for _, a := range []byte("abcdefghijklmnopqrstuvwxyz") { + content := []byte{a} + name := string(content) + err := stor.Put(name, bytes.NewBuffer(content), + int64(len(content))) + c.Assert(err, gc.IsNil) + } + reader, err := storage.Get(stor, "a") + c.Assert(err, gc.IsNil) + allContent, err := ioutil.ReadAll(reader) + c.Assert(err, gc.IsNil) + c.Assert(string(allContent), gc.Equals, "a") + err = stor.RemoveAll() + c.Assert(err, gc.IsNil) + _, err = storage.Get(stor, "a") + c.Assert(err, gc.NotNil) +} + +func (s *localServerSuite) TestDeleteMoreThan100(c *gc.C) { + env := s.Prepare(c) + stor := env.Storage() + // 6*26 = 156 items + for _, a := range []byte("abcdef") { + for _, b := range []byte("abcdefghijklmnopqrstuvwxyz") { + content := []byte{a, b} + name := string(content) + err := stor.Put(name, bytes.NewBuffer(content), + int64(len(content))) + c.Assert(err, gc.IsNil) + } + } + reader, err := storage.Get(stor, "ab") + c.Assert(err, gc.IsNil) + allContent, err := ioutil.ReadAll(reader) + c.Assert(err, gc.IsNil) + c.Assert(string(allContent), gc.Equals, "ab") + err = stor.RemoveAll() + c.Assert(err, gc.IsNil) + _, err = storage.Get(stor, "ab") + c.Assert(err, gc.NotNil) +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/provider.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/provider.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/provider.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/provider.go 2014-04-04 16:53:35.000000000 +0000 @@ -9,34 +9,32 @@ "github.com/juju/loggo" + "github.com/joyent/gosign/auth" "launchpad.net/juju-core/environs" "launchpad.net/juju-core/environs/config" + "launchpad.net/juju-core/environs/imagemetadata" + "launchpad.net/juju-core/environs/simplestreams" + envtools "launchpad.net/juju-core/environs/tools" ) var logger = loggo.GetLogger("juju.provider.joyent") -type environProvider struct{} +type joyentProvider struct{} -var providerInstance = environProvider{} +var providerInstance = joyentProvider{} var _ environs.EnvironProvider = providerInstance +var _ simplestreams.HasRegion = (*joyentEnviron)(nil) +var _ imagemetadata.SupportsCustomSources = (*joyentEnviron)(nil) +var _ envtools.SupportsCustomSources = (*joyentEnviron)(nil) + func init() { - // This will only happen in binaries that actually import this provider - // somewhere. To enable a provider, import it in the "providers/all" - // package; please do *not* import individual providers anywhere else, - // except for tests for that provider. environs.RegisterProvider("joyent", providerInstance) } var errNotImplemented = errors.New("not implemented in Joyent provider") -func (environProvider) Prepare(ctx environs.BootstrapContext, cfg *config.Config) (environs.Environ, error) { - // This method may be called with an incomplete cfg. It should make every - // reasonable effort to create a valid configuration based on the supplied, - // and open the resulting environment. - // You should implement this method to the best of your ability before - // expecting non-developers to use your provider, but it shouldn't be your - // first priority. +func (joyentProvider) Prepare(ctx environs.BootstrapContext, cfg *config.Config) (environs.Environ, error) { preparedCfg, err := prepareConfig(cfg) if err != nil { return nil, err @@ -44,35 +42,37 @@ return providerInstance.Open(preparedCfg) } -func (environProvider) Open(cfg *config.Config) (environs.Environ, error) { - env := &environ{name: cfg.Name()} - if err := env.SetConfig(cfg); err != nil { +func credentials(cfg *environConfig) (*auth.Credentials, error) { + if cfg.privateKey() == "" { + return nil, errors.New("cannot create credentials without a private key") + } + authentication := auth.Auth{User: cfg.mantaUser(), PrivateKey: cfg.privateKey(), Algorithm: cfg.algorithm()} + return &auth.Credentials{ + UserAuthentication: authentication, + MantaKeyId: cfg.mantaKeyId(), + MantaEndpoint: auth.Endpoint{URL: cfg.mantaUrl()}, + SdcKeyId: cfg.sdcKeyId(), + SdcEndpoint: auth.Endpoint{URL: cfg.sdcUrl()}, + }, nil +} + +func (joyentProvider) Open(cfg *config.Config) (environs.Environ, error) { + env, err := newEnviron(cfg) + if err != nil { return nil, err } return env, nil } -func (environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) { - // You should almost certainly not change this method; if you need to change - // how configs are validated, you should edit validateConfig itself, to ensure - // that your checks are always applied. - newEcfg, err := validateConfig(cfg, nil) +func (joyentProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) { + newEcfg, err := validateConfig(cfg, old) if err != nil { return nil, fmt.Errorf("invalid Joyent provider config: %v", err) } - if old != nil { - oldEcfg, err := validateConfig(old, nil) - if err != nil { - return nil, fmt.Errorf("original Joyent provider config is invalid: %v", err) - } - if newEcfg, err = validateConfig(cfg, oldEcfg); err != nil { - return nil, fmt.Errorf("invalid Joyent provider config change: %v", err) - } - } - return newEcfg.Config, nil + return cfg.Apply(newEcfg.attrs) } -func (environProvider) SecretAttrs(cfg *config.Config) (map[string]string, error) { +func (joyentProvider) SecretAttrs(cfg *config.Config) (map[string]string, error) { // If you keep configSecretFields up to date, this method should Just Work. ecfg, err := validateConfig(cfg, nil) if err != nil { @@ -97,24 +97,31 @@ return secretAttrs, nil } -func (environProvider) BoilerplateConfig() string { +func (joyentProvider) BoilerplateConfig() string { return boilerplateConfig } -func (environProvider) PublicAddress() (string, error) { - // Don't bother implementing this method until you're ready to deploy units. - // You probably won't need to by that stage; it's due for retirement. If it - // turns out that you do need to, remember that this method will *only* be - // called in code running on an instance in an environment using this - // provider; and it needs to return the address of *that* instance. - return "", errNotImplemented -} - -func (environProvider) PrivateAddress() (string, error) { - // Don't bother implementing this method until you're ready to deploy units. - // You probably won't need to by that stage; it's due for retirement. If it - // turns out that you do need to, remember that this method will *only* be - // called in code running on an instance in an environment using this - // provider; and it needs to return the address of *that* instance. - return "", errNotImplemented + +func GetProviderInstance() environs.EnvironProvider { + return providerInstance +} + +// MetadataLookupParams returns parameters which are used to query image metadata to +// find matching image information. +func (p joyentProvider) MetadataLookupParams(region string) (*simplestreams.MetadataLookupParams, error) { + if region == "" { + return nil, fmt.Errorf("region must be specified") + } + return &simplestreams.MetadataLookupParams{ + Region: region, + Architectures: []string{"amd64", "armhf"}, + }, nil +} + +func (p joyentProvider) newConfig(cfg *config.Config) (*environConfig, error) { + valid, err := p.Validate(cfg, nil) + if err != nil { + return nil, err + } + return &environConfig{valid, valid.UnknownAttrs()}, nil } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/provider_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/provider_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/provider_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/provider_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -0,0 +1,21 @@ +// Copyright 2013 Joyent Inc. +// Licensed under the AGPLv3, see LICENCE file for details. + +package joyent_test + +import ( + "flag" + "testing" + + gc "launchpad.net/gocheck" +) + +var live = flag.Bool("live", false, "Also run tests on live Joyent Public Cloud") + +func TestJoyent(t *testing.T) { + if *live { + registerLiveTests() + } + registerLocalTests() + gc.TestingT(t) +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/storage.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/storage.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/storage.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/storage.go 2014-04-04 16:53:35.000000000 +0000 @@ -4,50 +4,278 @@ package joyent import ( + "bytes" + "fmt" "io" + "io/ioutil" + "path" + "strings" + "sync" + "time" "launchpad.net/juju-core/environs/storage" + coreerrors "launchpad.net/juju-core/errors" "launchpad.net/juju-core/utils" + + "github.com/joyent/gocommon/client" + je "github.com/joyent/gocommon/errors" + "github.com/joyent/gomanta/manta" ) -type environStorage struct { - ecfg *environConfig +type JoyentStorage struct { + sync.Mutex + ecfg *environConfig + madeContainer bool + containerName string + manta *manta.Client +} + +type byteCloser struct { + io.Reader +} + +func (byteCloser) Close() error { + return nil +} + +var _ storage.Storage = (*JoyentStorage)(nil) + +func newStorage(cfg *environConfig, name string) (storage.Storage, error) { + creds, err := credentials(cfg) + if err != nil { + return nil, err + } + client := client.NewClient(cfg.mantaUrl(), "", creds, &logger) + + if name == "" { + name = cfg.controlDir() + } + + return &JoyentStorage{ + ecfg: cfg, + containerName: name, + manta: manta.New(client)}, nil +} + +func (s *JoyentStorage) GetContainerName() string { + return s.containerName } -var _ storage.Storage = (*environStorage)(nil) +func (s *JoyentStorage) GetMantaUrl() string { + return s.ecfg.mantaUrl() +} -func newStorage(ecfg *environConfig) (storage.Storage, error) { - return &environStorage{ecfg}, nil +func (s *JoyentStorage) GetMantaUser() string { + return s.ecfg.mantaUser() } -func (s *environStorage) List(prefix string) ([]string, error) { - return nil, errNotImplemented +// createContainer makes the environment's control container, the +// place where bootstrap information and deployed charms +// are stored. To avoid two round trips on every PUT operation, +// we do this only once for each environ. +func (s *JoyentStorage) createContainer() error { + s.Lock() + defer s.Unlock() + if s.madeContainer { + return nil + } + // try to make the container + err := s.manta.PutDirectory(s.containerName) + if err == nil { + s.madeContainer = true + } + return err } -func (s *environStorage) URL(name string) (string, error) { - return "", errNotImplemented +// deleteContainer deletes the named container from the storage account. +func (s *JoyentStorage) DeleteContainer(containerName string) error { + err := s.manta.DeleteDirectory(containerName) + if err == nil && strings.EqualFold(s.containerName, containerName) { + s.madeContainer = false + } + if je.IsResourceNotFound(err) { + return coreerrors.NewNotFoundError(err, fmt.Sprintf("cannot delete %s, not found", containerName)) + } + return err } -func (s *environStorage) Get(name string) (io.ReadCloser, error) { - return nil, errNotImplemented +func (s *JoyentStorage) List(prefix string) ([]string, error) { + content, err := list(s, s.containerName) + if err != nil { + return nil, err + } + + var names []string + for _, item := range content { + name := strings.TrimPrefix(item, s.containerName+"/") + if prefix != "" { + if strings.HasPrefix(name, prefix) { + names = append(names, name) + } + } else { + names = append(names, name) + } + } + return names, nil } -func (s *environStorage) Put(name string, r io.Reader, length int64) error { - return errNotImplemented +func list(s *JoyentStorage, path string) ([]string, error) { + // TODO - we don't want to create the container here, but instead handle + // any 404 and return as if no files exist. + if err := s.createContainer(); err != nil { + return nil, fmt.Errorf("cannot make Manta control container: %v", err) + } + // use empty opts, i.e. default values + // -- might be added in the provider config? + contents, err := s.manta.ListDirectory(path, manta.ListDirectoryOpts{}) + if err != nil { + return nil, err + } + var names []string + for _, item := range contents { + if strings.EqualFold(item.Type, "directory") { + items, err := list(s, path+"/"+item.Name) + if err != nil { + return nil, err + } + names = append(names, items...) + } else { + names = append(names, path+"/"+item.Name) + } + } + return names, nil } -func (s *environStorage) Remove(name string) error { - return errNotImplemented +//return something that a random wget can retrieve the object at, without any credentials +func (s *JoyentStorage) URL(name string) (string, error) { + path := fmt.Sprintf("/%s/stor/%s/%s", s.ecfg.mantaUser(), s.containerName, name) + return s.manta.SignURL(path, time.Now().AddDate(10, 0, 0)) +} + +func (s *JoyentStorage) Get(name string) (io.ReadCloser, error) { + b, err := s.manta.GetObject(s.containerName, name) + if err != nil { + return nil, coreerrors.NewNotFoundError(err, fmt.Sprintf("cannot find %s", name)) + } + r := byteCloser{bytes.NewReader(b)} + return r, nil +} + +func (s *JoyentStorage) Put(name string, r io.Reader, length int64) error { + if err := s.createContainer(); err != nil { + return fmt.Errorf("cannot make Manta control container: %v", err) + } + if strings.Contains(name, "/") { + var parents []string + dirs := strings.Split(name, "/") + for i, _ := range dirs { + if i < (len(dirs) - 1) { + parents = append(parents, strings.Join(dirs[:(i+1)], "/")) + } + } + for _, dir := range parents { + err := s.manta.PutDirectory(path.Join(s.containerName, dir)) + if err != nil { + return fmt.Errorf("cannot create parent directory %q in control container %q: %v", dir, s.containerName, err) + } + } + } + object, err := ioutil.ReadAll(r) + if err != nil { + return fmt.Errorf("failed to read object %q: %v", name, err) + } + err = s.manta.PutObject(s.containerName, name, object) + if err != nil { + return fmt.Errorf("cannot write file %q to control container %q: %v", name, s.containerName, err) + } + return nil +} + +func (s *JoyentStorage) Remove(name string) error { + err := s.manta.DeleteObject(s.containerName, name) + if err != nil { + if je.IsResourceNotFound(err) { + // gojoyent returns an error if file doesn't exist + // just log a warning + logger.Warningf("cannot delete %s from %s, already deleted", name, s.containerName) + } else { + return err + } + } + + if strings.Contains(name, "/") { + var parents []string + dirs := strings.Split(name, "/") + for i := (len(dirs) - 1); i >= 0; i-- { + if i < (len(dirs) - 1) { + parents = append(parents, strings.Join(dirs[:(i+1)], "/")) + } + } + + for _, dir := range parents { + err := s.manta.DeleteDirectory(path.Join(s.containerName, dir)) + if err != nil { + if je.IsBadRequest(err) { + // check if delete request returned a bad request error, i.e. directory is not empty + // just log a warning + logger.Warningf("cannot delete %s, not empty", dir) + } else if je.IsResourceNotFound(err) { + // check if delete request returned a resource not found error, i.e. directory was already deleted + // just log a warning + logger.Warningf("cannot delete %s, already deleted", dir) + } else { + return fmt.Errorf("cannot delete parent directory %q in control container %q: %v", dir, s.containerName, err) + } + } + } + } + + return nil } -func (s *environStorage) RemoveAll() error { - return errNotImplemented +func (s *JoyentStorage) RemoveAll() error { + names, err := storage.List(s, "") + if err != nil { + return err + } + // Remove all the objects in parallel so that we incur less round-trips. + // If we're in danger of having hundreds of objects, + // we'll want to change this to limit the number + // of concurrent operations. + var wg sync.WaitGroup + wg.Add(len(names)) + errc := make(chan error, len(names)) + for _, name := range names { + name := name + go func() { + defer wg.Done() + if err := s.Remove(name); err != nil { + errc <- err + } + }() + } + wg.Wait() + select { + case err := <-errc: + return fmt.Errorf("cannot delete all provider state: %v", err) + default: + } + + s.Lock() + defer s.Unlock() + // Even DeleteContainer fails, it won't harm if we try again - the + // operation might have succeeded even if we get an error. + s.madeContainer = false + if err = s.manta.DeleteDirectory(s.containerName); err != nil { + return err + } + return nil } -func (s *environStorage) DefaultConsistencyStrategy() utils.AttemptStrategy { +func (s *JoyentStorage) DefaultConsistencyStrategy() utils.AttemptStrategy { return utils.AttemptStrategy{} } -func (s *environStorage) ShouldRetry(err error) bool { +func (s *JoyentStorage) ShouldRetry(err error) bool { return false } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/storage_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/storage_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/joyent/storage_test.go 1970-01-01 00:00:00.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/joyent/storage_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -0,0 +1,199 @@ +// Copyright 2013 Joyent Inc. +// Licensed under the AGPLv3, see LICENCE file for details. + +package joyent_test + +import ( + "fmt" + "io/ioutil" + "math/rand" + "net/http" + "net/url" + "strings" + + "github.com/joyent/gocommon/errors" + jc "github.com/juju/testing/checkers" + gc "launchpad.net/gocheck" + + coreerrors "launchpad.net/juju-core/errors" + "launchpad.net/juju-core/provider/joyent" + jp "launchpad.net/juju-core/provider/joyent" +) + +type storageSuite struct { + providerSuite + localMantaServer +} + +const ( + storageName = "testStorage" + fileName = "testFile" + fileBlobContent = "Juju Joyent Provider Storage - Test" +) + +var _ = gc.Suite(&storageSuite{}) + +func (s *storageSuite) SetUpSuite(c *gc.C) { + s.providerSuite.SetUpSuite(c) + s.localMantaServer.setupServer(c) +} + +func (s *storageSuite) TearDownSuite(c *gc.C) { + s.localMantaServer.destroyServer() + s.providerSuite.TearDownSuite(c) +} + +// makeStorage creates a Manta storage object for the running test. +func (s *storageSuite) makeStorage(name string, c *gc.C) *jp.JoyentStorage { + stor := joyent.MakeStorage(c, GetFakeConfig("localhost", s.localMantaServer.Server.URL)) + return stor.(*jp.JoyentStorage) +} + +func (s *storageSuite) assertContainer(storage *jp.JoyentStorage, c *gc.C) { + err := jp.CreateContainer(storage) + c.Assert(err, gc.IsNil) +} + +func (s *storageSuite) assertFile(storage *jp.JoyentStorage, c *gc.C) { + err := storage.Put(fileName, strings.NewReader(fileBlobContent), int64(len(fileBlobContent))) + c.Assert(err, gc.IsNil) +} + +// makeRandomBytes returns an array of arbitrary byte values. +func makeRandomBytes(length int) []byte { + data := make([]byte, length) + for index := range data { + data[index] = byte(rand.Intn(256)) + } + return data +} + +func makeResponse(content string, status int) *http.Response { + return &http.Response{ + Status: fmt.Sprintf("%d", status), + StatusCode: status, + Body: ioutil.NopCloser(strings.NewReader(content)), + } +} + +func (s *storageSuite) TestList(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + s.assertContainer(mantaStorage, c) + s.assertFile(mantaStorage, c) + + names, err := mantaStorage.List("") + c.Assert(err, gc.IsNil) + c.Check(names, gc.DeepEquals, []string{fileName}) +} + +func (s *storageSuite) TestListWithPrefix(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + s.assertContainer(mantaStorage, c) + s.assertFile(mantaStorage, c) + err := mantaStorage.Put("pr/fileName", strings.NewReader(fileBlobContent), int64(len(fileBlobContent))) + c.Assert(err, gc.IsNil) + + names, err := mantaStorage.List("p") + c.Assert(err, gc.IsNil) + c.Check(names, gc.DeepEquals, []string{"pr/fileName"}) +} + +func (s *storageSuite) TestGet(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + s.assertFile(mantaStorage, c) + + reader, err := mantaStorage.Get(fileName) + c.Assert(err, gc.IsNil) + c.Assert(reader, gc.NotNil) + defer reader.Close() + + data, err := ioutil.ReadAll(reader) + c.Assert(err, gc.IsNil) + c.Check(string(data), gc.Equals, fileBlobContent) +} + +func (s *storageSuite) TestGetFileNotExists(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + + _, err := mantaStorage.Get("noFile") + c.Assert(err, gc.NotNil) + c.Assert(err, jc.Satisfies, coreerrors.IsNotFoundError) +} + +func (s *storageSuite) TestPut(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + + s.assertFile(mantaStorage, c) +} + +func (s *storageSuite) TestRemove(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + s.assertFile(mantaStorage, c) + + err := mantaStorage.Remove(fileName) + c.Assert(err, gc.IsNil) +} + +func (s *storageSuite) TestRemoveFileNotExists(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + + err := mantaStorage.Remove("nofile") + c.Assert(err, gc.IsNil) +} + +func (s *storageSuite) TestRemoveAll(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + + err := mantaStorage.RemoveAll() + c.Assert(err, gc.IsNil) +} + +func (s *storageSuite) TestURL(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + + URL, err := mantaStorage.URL(fileName) + c.Assert(err, gc.IsNil) + parsedURL, err := url.Parse(URL) + c.Assert(err, gc.IsNil) + c.Check(parsedURL.Host, gc.Matches, mantaStorage.GetMantaUrl()[strings.LastIndex(mantaStorage.GetMantaUrl(), "/")+1:]) + c.Check(parsedURL.Path, gc.Matches, fmt.Sprintf("/%s/stor/%s/%s", mantaStorage.GetMantaUser(), mantaStorage.GetContainerName(), fileName)) +} + +func (s *storageSuite) TestCreateContainer(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + + s.assertContainer(mantaStorage, c) +} + +func (s *storageSuite) TestCreateContainerAlreadyExists(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + + s.assertContainer(mantaStorage, c) + s.assertContainer(mantaStorage, c) +} + +func (s *storageSuite) TestDeleteContainer(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + s.assertContainer(mantaStorage, c) + + err := mantaStorage.DeleteContainer(mantaStorage.GetContainerName()) + c.Assert(err, gc.IsNil) +} + +func (s *storageSuite) TestDeleteContainerNotEmpty(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + s.assertContainer(mantaStorage, c) + s.assertFile(mantaStorage, c) + + err := mantaStorage.DeleteContainer(mantaStorage.GetContainerName()) + c.Assert(err, gc.NotNil) + c.Assert(err, jc.Satisfies, errors.IsBadRequest) +} + +func (s *storageSuite) TestDeleteContainerNotExists(c *gc.C) { + mantaStorage := s.makeStorage(storageName, c) + + err := mantaStorage.DeleteContainer("noContainer") + c.Assert(err, gc.NotNil) + c.Assert(err, jc.Satisfies, coreerrors.IsNotFoundError) +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/local/environprovider.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/local/environprovider.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/local/environprovider.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/local/environprovider.go 2014-04-04 16:57:31.000000000 +0000 @@ -265,22 +265,6 @@ return nil, nil } -// Location specific methods that are able to be called by any instance that -// has been created by this provider type. So a machine agent may well call -// these methods to find out its own address or instance id. - -// PublicAddress implements environs.EnvironProvider.PublicAddress. -func (environProvider) PublicAddress() (string, error) { - // Get the IPv4 address from eth0 - return getAddressForInterface("eth0") -} - -// PrivateAddress implements environs.EnvironProvider.PrivateAddress. -func (environProvider) PrivateAddress() (string, error) { - // Get the IPv4 address from eth0 - return getAddressForInterface("eth0") -} - func (p environProvider) newConfig(cfg *config.Config) (*environConfig, error) { valid, err := p.Validate(cfg, nil) if err != nil { diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/maas/environprovider.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/maas/environprovider.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/maas/environprovider.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/maas/environprovider.go 2014-04-04 16:53:35.000000000 +0000 @@ -5,14 +5,11 @@ import ( "errors" - "os" "github.com/juju/loggo" "launchpad.net/juju-core/environs" "launchpad.net/juju-core/environs/config" - "launchpad.net/juju-core/instance" - "launchpad.net/juju-core/juju/osenv" "launchpad.net/juju-core/utils" ) @@ -91,26 +88,3 @@ secretAttrs["maas-oauth"] = maasCfg.maasOAuth() return secretAttrs, nil } - -func (maasEnvironProvider) hostname() (string, error) { - // Hack to get container ip addresses properly for MAAS demo. - if os.Getenv(osenv.JujuContainerTypeEnvKey) == string(instance.LXC) { - return utils.GetAddressForInterface("eth0") - } - info := machineInfo{} - err := info.load() - if err != nil { - return "", err - } - return info.Hostname, nil -} - -// PublicAddress is specified in the EnvironProvider interface. -func (prov maasEnvironProvider) PublicAddress() (string, error) { - return prov.hostname() -} - -// PrivateAddress is specified in the EnvironProvider interface. -func (prov maasEnvironProvider) PrivateAddress() (string, error) { - return prov.hostname() -} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/maas/environprovider_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/maas/environprovider_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/maas/environprovider_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/maas/environprovider_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -8,7 +8,6 @@ jc "github.com/juju/testing/checkers" gc "launchpad.net/gocheck" - "launchpad.net/goyaml" "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/juju/osenv" @@ -93,31 +92,6 @@ return filename } -// PublicAddress and PrivateAddress return the hostname of the machine read -// from the file _MAASInstanceFilename. -func (suite *EnvironProviderSuite) TestPrivatePublicAddressReadsHostnameFromMachineFile(c *gc.C) { - hostname := "myhostname" - info := machineInfo{hostname} - yaml, err := goyaml.Marshal(info) - c.Assert(err, gc.IsNil) - // Create a temporary file to act as the file where the instanceID - // is stored. - filename := createTempFile(c, yaml) - // "Monkey patch" the value of _MAASInstanceFilename with the path - // to the temporary file. - old_MAASInstanceFilename := _MAASInstanceFilename - _MAASInstanceFilename = filename - defer func() { _MAASInstanceFilename = old_MAASInstanceFilename }() - - provider := suite.makeEnviron().Provider() - publicAddress, err := provider.PublicAddress() - c.Assert(err, gc.IsNil) - c.Check(publicAddress, gc.Equals, hostname) - privateAddress, err := provider.PrivateAddress() - c.Assert(err, gc.IsNil) - c.Check(privateAddress, gc.Equals, hostname) -} - func (suite *EnvironProviderSuite) TestOpenReturnsNilInterfaceUponFailure(c *gc.C) { testJujuHome := c.MkDir() defer osenv.SetJujuHome(osenv.SetJujuHome(testJujuHome)) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/manual/provider.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/manual/provider.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/manual/provider.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/manual/provider.go 2014-04-04 16:53:35.000000000 +0000 @@ -169,16 +169,3 @@ attrs["storage-auth-key"] = envConfig.storageAuthKey() return attrs, nil } - -func (_ manualProvider) PublicAddress() (string, error) { - // TODO(axw) 2013-09-10 bug #1222643 - // - // eth0 may not be the desired interface for traffic to route - // through. We should somehow make this configurable, and - // possibly also record the IP resolved during manual bootstrap. - return utils.GetAddressForInterface("eth0") -} - -func (p manualProvider) PrivateAddress() (string, error) { - return p.PublicAddress() -} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/openstack/export_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/openstack/export_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/openstack/export_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/openstack/export_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -32,32 +32,6 @@ testRoundTripper.RegisterForScheme("test") } -var origMetadataHost = metadataHost - -var metadataContent = `"availability_zone": "nova", "hostname": "test.novalocal", ` + - `"launch_index": 0, "meta": {"priority": "low", "role": "webserver"}, ` + - `"public_keys": {"mykey": "ssh-rsa fake-key\n"}, "name": "test"}` - -// A group of canned responses for the "metadata server". These match -// reasonably well with the results of making those requests on a Folsom+ -// Openstack service -var MetadataTesting = map[string]string{ - "/latest/meta-data/local-ipv4": "10.1.1.2", - "/latest/meta-data/public-ipv4": "203.1.1.2", - "/openstack/2012-08-10/meta_data.json": metadataContent, -} - -// Set Metadata requests to be served by the filecontent supplied. -func UseTestMetadata(metadata map[string]string) { - if len(metadata) != 0 { - testRoundTripper.Sub = jujutest.NewCannedRoundTripper(metadata, nil) - metadataHost = "test:" - } else { - testRoundTripper.Sub = nil - metadataHost = origMetadataHost - } -} - var ( ShortAttempt = &shortAttempt StorageAttempt = &storageAttempt diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/openstack/local_test.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/openstack/local_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/openstack/local_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/openstack/local_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -54,44 +54,6 @@ s.restoreTimeouts() } -func (s *ProviderSuite) TestMetadata(c *gc.C) { - openstack.UseTestMetadata(openstack.MetadataTesting) - defer openstack.UseTestMetadata(nil) - - p, err := environs.Provider("openstack") - c.Assert(err, gc.IsNil) - - addr, err := p.PublicAddress() - c.Assert(err, gc.IsNil) - c.Assert(addr, gc.Equals, "203.1.1.2") - - addr, err = p.PrivateAddress() - c.Assert(err, gc.IsNil) - c.Assert(addr, gc.Equals, "10.1.1.2") -} - -func (s *ProviderSuite) TestPublicFallbackToPrivate(c *gc.C) { - openstack.UseTestMetadata(map[string]string{ - "/latest/meta-data/public-ipv4": "203.1.1.2", - "/latest/meta-data/local-ipv4": "10.1.1.2", - }) - defer openstack.UseTestMetadata(nil) - p, err := environs.Provider("openstack") - c.Assert(err, gc.IsNil) - - addr, err := p.PublicAddress() - c.Assert(err, gc.IsNil) - c.Assert(addr, gc.Equals, "203.1.1.2") - - openstack.UseTestMetadata(map[string]string{ - "/latest/meta-data/local-ipv4": "10.1.1.2", - "/latest/meta-data/public-ipv4": "", - }) - addr, err = p.PublicAddress() - c.Assert(err, gc.IsNil) - c.Assert(addr, gc.Equals, "10.1.1.2") -} - // Register tests to run against a test Openstack instance (service doubles). func registerLocalTests() { cred := &identity.Credentials{ diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/provider/openstack/provider.go juju-core-1.18.0/src/launchpad.net/juju-core/provider/openstack/provider.go --- juju-core-1.17.7/src/launchpad.net/juju-core/provider/openstack/provider.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/provider/openstack/provider.go 2014-04-04 16:57:31.000000000 +0000 @@ -11,7 +11,6 @@ "io/ioutil" "net/http" "regexp" - "strings" "sync" "time" @@ -250,36 +249,6 @@ return m, nil } -func (p environProvider) PublicAddress() (string, error) { - if addr, err := fetchMetadata("public-ipv4"); err != nil { - return "", err - } else if addr != "" { - return addr, nil - } - return p.PrivateAddress() -} - -func (p environProvider) PrivateAddress() (string, error) { - return fetchMetadata("local-ipv4") -} - -// metadataHost holds the address of the instance metadata service. -// It is a variable so that tests can change it to refer to a local -// server when needed. -var metadataHost = "http://169.254.169.254" - -// fetchMetadata fetches a single atom of data from the openstack instance metadata service. -// http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html -// (the same specs is implemented in ec2, hence the reference) -func fetchMetadata(name string) (value string, err error) { - uri := fmt.Sprintf("%s/latest/meta-data/%s", metadataHost, name) - data, err := retryGet(uri) - if err != nil { - return "", err - } - return strings.TrimSpace(string(data)), nil -} - func retryGet(uri string) (data []byte, err error) { for a := shortAttempt.Start(); a.Next(); { var resp *http.Response @@ -1253,7 +1222,7 @@ return nil, err } return &simplestreams.MetadataLookupParams{ - Series: e.ecfg().DefaultSeries(), + Series: config.PreferredSeries(e.ecfg()), Region: cloudSpec.Region, Endpoint: cloudSpec.Endpoint, Architectures: arch.AllSupportedArches, diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/scripts/win-installer/setup.iss juju-core-1.18.0/src/launchpad.net/juju-core/scripts/win-installer/setup.iss --- juju-core-1.17.7/src/launchpad.net/juju-core/scripts/win-installer/setup.iss 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/scripts/win-installer/setup.iss 2014-04-04 16:57:31.000000000 +0000 @@ -2,7 +2,7 @@ ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! #define MyAppName "Juju" -#define MyAppVersion "1.17.7" +#define MyAppVersion "1.18.0" #define MyAppPublisher "Canonical, Ltd" #define MyAppURL "http://juju.ubuntu.com/" #define MyAppExeName "juju.exe" diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/state/api/client.go juju-core-1.18.0/src/launchpad.net/juju-core/state/api/client.go --- juju-core-1.17.7/src/launchpad.net/juju-core/state/api/client.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/state/api/client.go 2014-04-04 16:57:31.000000000 +0000 @@ -608,6 +608,24 @@ return c.call("AddCharm", args, nil) } +// ResolveCharms resolves the best available charm URLs with series, for charm +// locations without a series specified. +func (c *Client) ResolveCharm(ref charm.Reference) (*charm.URL, error) { + args := params.ResolveCharms{References: []charm.Reference{ref}} + result := new(params.ResolveCharmResults) + if err := c.st.Call("Client", "", "ResolveCharms", args, result); err != nil { + return nil, err + } + if len(result.URLs) == 0 { + return nil, fmt.Errorf("unexpected empty response") + } + urlInfo := result.URLs[0] + if urlInfo.Error != "" { + return nil, fmt.Errorf("%v", urlInfo.Error) + } + return urlInfo.URL, nil +} + func (c *Client) UploadTools( toolsFilename string, vers version.Binary, fakeSeries ...string, ) ( diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/state/api/params/params.go juju-core-1.18.0/src/launchpad.net/juju-core/state/api/params/params.go --- juju-core-1.17.7/src/launchpad.net/juju-core/state/api/params/params.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/state/api/params/params.go 2014-04-04 16:57:31.000000000 +0000 @@ -330,6 +330,22 @@ CharmURL string } +// ResolveCharms stores charm references for a ResolveCharms call. +type ResolveCharms struct { + References []charm.Reference +} + +// ResolveCharmResult holds the result of resolving a charm reference to a URL, or any error that occurred. +type ResolveCharmResult struct { + URL *charm.URL `json:",omitempty"` + Error string `json:",omitempty"` +} + +// ResolveCharmResults holds results of the ResolveCharms call. +type ResolveCharmResults struct { + URLs []ResolveCharmResult +} + // AllWatcherId holds the id of an AllWatcher. type AllWatcherId struct { AllWatcherId string diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/state/api/params/params_test.go juju-core-1.18.0/src/launchpad.net/juju-core/state/api/params/params_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/state/api/params/params_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/state/api/params/params_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -58,14 +58,14 @@ Life: params.Dying, OwnerTag: "test-owner", MinUnits: 42, - Constraints: constraints.MustParse("arch=arm mem=1024M"), + Constraints: constraints.MustParse("arch=armhf mem=1024M"), Config: charm.Settings{ "hello": "goodbye", "foo": false, }, }, }, - json: `["service","change",{"CharmURL": "cs:quantal/name","Name":"Benji","Exposed":true,"Life":"dying","OwnerTag":"test-owner","MinUnits":42,"Constraints":{"arch":"arm", "mem": 1024},"Config": {"hello":"goodbye","foo":false}}]`, + json: `["service","change",{"CharmURL": "cs:quantal/name","Name":"Benji","Exposed":true,"Life":"dying","OwnerTag":"test-owner","MinUnits":42,"Constraints":{"arch":"armhf", "mem": 1024},"Config": {"hello":"goodbye","foo":false}}]`, }, { about: "UnitInfo Delta", value: params.Delta{ diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/state/apiserver/client/client.go juju-core-1.18.0/src/launchpad.net/juju-core/state/apiserver/client/client.go --- juju-core-1.17.7/src/launchpad.net/juju-core/state/apiserver/client/client.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/state/apiserver/client/client.go 2014-04-04 16:57:31.000000000 +0000 @@ -567,16 +567,8 @@ results := params.AddMachinesResults{ Machines: make([]params.AddMachinesResult, len(args.MachineParams)), } - - var defaultSeries string - conf, err := c.api.state.EnvironConfig() - if err != nil { - return results, err - } - defaultSeries = conf.DefaultSeries() - for i, p := range args.MachineParams { - m, err := c.addOneMachine(p, defaultSeries) + m, err := c.addOneMachine(p) results.Machines[i].Error = common.ServerError(err) if err == nil { results.Machines[i].Machine = m.Id() @@ -590,9 +582,13 @@ return c.AddMachines(args) } -func (c *Client) addOneMachine(p params.AddMachineParams, defaultSeries string) (*state.Machine, error) { +func (c *Client) addOneMachine(p params.AddMachineParams) (*state.Machine, error) { if p.Series == "" { - p.Series = defaultSeries + conf, err := c.api.state.EnvironConfig() + if err != nil { + return nil, err + } + p.Series = config.PreferredSeries(conf) } if p.ContainerType != "" { // Guard against dubious client by making sure that @@ -709,7 +705,7 @@ } info := api.EnvironmentInfo{ - DefaultSeries: conf.DefaultSeries(), + DefaultSeries: config.PreferredSeries(conf), ProviderType: conf.Type(), Name: conf.Name(), UUID: env.UUID(), @@ -955,6 +951,37 @@ return err } +func (c *Client) ResolveCharms(args params.ResolveCharms) (params.ResolveCharmResults, error) { + var results params.ResolveCharmResults + + envConfig, err := c.api.state.EnvironConfig() + if err != nil { + return params.ResolveCharmResults{}, err + } + repo := config.SpecializeCharmRepo(CharmStore, envConfig) + + for _, ref := range args.References { + result := params.ResolveCharmResult{} + curl, err := c.resolveCharm(ref, repo) + if err != nil { + result.Error = err.Error() + } else { + result.URL = curl + } + results.URLs = append(results.URLs, result) + } + return results, nil +} + +func (c *Client) resolveCharm(ref charm.Reference, repo charm.Repository) (*charm.URL, error) { + if ref.Schema != "cs" { + return nil, fmt.Errorf("only charm store charm references are supported, with cs: schema") + } + + // Resolve the charm location with the repository. + return repo.Resolve(ref) +} + // CharmArchiveName returns a string that is suitable as a file name // in a storage URL. It is constructed from the charm name, revision // and a random UUID string. diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/state/apiserver/client/client_test.go juju-core-1.18.0/src/launchpad.net/juju-core/state/apiserver/client/client_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/state/apiserver/client/client_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/state/apiserver/client/client_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -261,7 +261,7 @@ c.Assert(err, gc.IsNil) env, err := s.State.Environment() c.Assert(err, gc.IsNil) - c.Assert(info.DefaultSeries, gc.Equals, conf.DefaultSeries()) + c.Assert(info.DefaultSeries, gc.Equals, config.PreferredSeries(conf)) c.Assert(info.ProviderType, gc.Equals, conf.Type()) c.Assert(info.Name, gc.Equals, conf.Name()) c.Assert(info.UUID, gc.Equals, env.UUID()) @@ -1119,8 +1119,12 @@ } func addCharm(c *gc.C, store *coretesting.MockCharmStore, name string) (*charm.URL, charm.Charm) { + return addSeriesCharm(c, store, "precise", name) +} + +func addSeriesCharm(c *gc.C, store *coretesting.MockCharmStore, series, name string) (*charm.URL, charm.Charm) { bundle := coretesting.Charms.Bundle(c.MkDir(), name) - scurl := fmt.Sprintf("cs:precise/%s-%d", name, bundle.Revision()) + scurl := fmt.Sprintf("cs:%s/%s-%d", series, name, bundle.Revision()) curl := charm.MustParseURL(scurl) err := store.SetCharm(curl, bundle) c.Assert(err, gc.IsNil) @@ -1640,7 +1644,7 @@ c.Assert(len(machines), gc.Equals, 3) for i, machineResult := range machines { c.Assert(machineResult.Machine, gc.DeepEquals, strconv.Itoa(i)) - s.checkMachine(c, machineResult.Machine, config.DefaultSeries, apiParams[i].Constraints.String()) + s.checkMachine(c, machineResult.Machine, coretesting.FakeDefaultSeries, apiParams[i].Constraints.String()) } } @@ -1690,7 +1694,7 @@ c.Assert(len(machines), gc.Equals, 3) for i, machineResult := range machines { c.Assert(machineResult.Machine, gc.DeepEquals, strconv.Itoa(i)) - s.checkMachine(c, machineResult.Machine, config.DefaultSeries, apiParams[i].Constraints.String()) + s.checkMachine(c, machineResult.Machine, coretesting.FakeDefaultSeries, apiParams[i].Constraints.String()) } } @@ -1761,7 +1765,7 @@ c.Assert(machineResult.Error, gc.ErrorMatches, "cannot add a new machine: cannot add a machine with an instance id and no nonce") } else { c.Assert(machineResult.Machine, gc.DeepEquals, strconv.Itoa(i)) - s.checkMachine(c, machineResult.Machine, config.DefaultSeries, apiParams[i].Constraints.String()) + s.checkMachine(c, machineResult.Machine, coretesting.FakeDefaultSeries, apiParams[i].Constraints.String()) instanceId := fmt.Sprintf("1234-%d", i) s.checkInstance(c, machineResult.Machine, instanceId, "foo", hc, addrs) } @@ -1949,6 +1953,57 @@ s.assertUploaded(c, storage, sch.BundleURL(), sch.BundleSha256()) } +var resolveCharmCases = []struct { + schema, defaultSeries, charmName string + parseErr string + resolveErr string +}{ + {"cs", "precise", "wordpress", "", ""}, + {"cs", "trusty", "wordpress", "", ""}, + {"cs", "", "wordpress", "", `missing default series, cannot resolve charm url: "cs:wordpress"`}, + {"cs", "trusty", "", `charm URL has invalid charm name: "cs:"`, ""}, + {"local", "trusty", "wordpress", "", `only charm store charm references are supported, with cs: schema`}, + {"cs", "precise", "hl3", "", ""}, + {"cs", "trusty", "hl3", "", ""}, + {"cs", "", "hl3", "", `missing default series, cannot resolve charm url: \"cs:hl3\"`}, +} + +func (s *clientSuite) TestResolveCharm(c *gc.C) { + store, restore := makeMockCharmStore() + defer restore() + + for i, test := range resolveCharmCases { + c.Logf("test %d: %#v", i, test) + // Mock charm store will use this to resolve a charm reference. + store.DefaultSeries = test.defaultSeries + + client := s.APIState.Client() + ref, series, err := charm.ParseReference(fmt.Sprintf("%s:%s", test.schema, test.charmName)) + if test.parseErr == "" { + if !c.Check(err, gc.IsNil) { + continue + } + } else { + c.Assert(err, gc.NotNil) + c.Check(err, gc.ErrorMatches, test.parseErr) + continue + } + c.Check(series, gc.Equals, "") + c.Check(ref.String(), gc.Equals, fmt.Sprintf("%s:%s", test.schema, test.charmName)) + + curl, err := client.ResolveCharm(ref) + if err == nil { + c.Assert(curl, gc.NotNil) + // Only cs: schema should make it through here + c.Check(curl.String(), gc.Equals, fmt.Sprintf("cs:%s/%s", test.defaultSeries, test.charmName)) + c.Check(test.resolveErr, gc.Equals, "") + } else { + c.Check(curl, gc.IsNil) + c.Check(err, gc.ErrorMatches, test.resolveErr) + } + } +} + func (s *clientSuite) TestAddCharmConcurrently(c *gc.C) { store, restore := makeMockCharmStore() defer restore() diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/state/machine.go juju-core-1.18.0/src/launchpad.net/juju-core/state/machine.go --- juju-core-1.17.7/src/launchpad.net/juju-core/state/machine.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/state/machine.go 2014-04-04 16:57:31.000000000 +0000 @@ -813,23 +813,28 @@ return ok } -// Addresses returns any hostnames and ips associated with a machine, -// determined both by the machine itself, and by asking the provider. -// -// The addresses returned by the provider shadow any of the addresses -// that the machine reported with the same address value. -func (m *Machine) Addresses() (addresses []instance.Address) { +func mergedAddresses(machineAddresses, providerAddresses []address) []instance.Address { merged := make(map[string]instance.Address) - for _, address := range m.doc.MachineAddresses { + for _, address := range machineAddresses { merged[address.Value] = address.InstanceAddress() } - for _, address := range m.doc.Addresses { + for _, address := range providerAddresses { merged[address.Value] = address.InstanceAddress() } + addresses := make([]instance.Address, 0, len(merged)) for _, address := range merged { addresses = append(addresses, address) } - return + return addresses +} + +// Addresses returns any hostnames and ips associated with a machine, +// determined both by the machine itself, and by asking the provider. +// +// The addresses returned by the provider shadow any of the addresses +// that the machine reported with the same address value. +func (m *Machine) Addresses() (addresses []instance.Address) { + return mergedAddresses(m.doc.MachineAddresses, m.doc.Addresses) } // SetAddresses records any addresses related to the machine, sourced diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/state/megawatcher.go juju-core-1.18.0/src/launchpad.net/juju-core/state/megawatcher.go --- juju-core-1.17.7/src/launchpad.net/juju-core/state/megawatcher.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/state/megawatcher.go 2014-04-04 16:53:35.000000000 +0000 @@ -32,7 +32,7 @@ Life: params.Life(m.Life.String()), Series: m.Series, Jobs: paramsJobsFromJobs(m.Jobs), - Addresses: addressesToInstanceAddresses(m.Addresses), + Addresses: mergedAddresses(m.MachineAddresses, m.Addresses), SupportedContainers: m.SupportedContainers, SupportedContainersKnown: m.SupportedContainersKnown, } @@ -87,13 +87,11 @@ func (u *backingUnit) updated(st *State, store *multiwatcher.Store, id interface{}) error { info := ¶ms.UnitInfo{ - Name: u.Name, - Service: u.Service, - Series: u.Series, - PublicAddress: u.PublicAddress, - PrivateAddress: u.PrivateAddress, - MachineId: u.MachineId, - Ports: u.Ports, + Name: u.Name, + Service: u.Service, + Series: u.Series, + MachineId: u.MachineId, + Ports: u.Ports, } if u.CharmURL != nil { info.CharmURL = u.CharmURL.String() @@ -114,10 +112,29 @@ info.Status = oldInfo.Status info.StatusInfo = oldInfo.StatusInfo } + publicAddress, privateAddress, err := getUnitAddresses(st, u.Name) + if err != nil { + return err + } + info.PublicAddress = publicAddress + info.PrivateAddress = privateAddress store.Update(info) return nil } +// getUnitAddresses returns the public and private addresses on a given unit. +// As of 1.18, the addresses are stored on the assigned machine but we retain +// this approach for backwards compatibility. +func getUnitAddresses(st *State, unitName string) (publicAddress, privateAddress string, err error) { + u, err := st.Unit(unitName) + if err != nil { + return "", "", err + } + publicAddress, _ = u.PublicAddress() + privateAddress, _ = u.PrivateAddress() + return publicAddress, privateAddress, nil +} + func (svc *backingUnit) removed(st *State, store *multiwatcher.Store, id interface{}) error { store.Remove(params.EntityId{ Kind: "unit", diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/state/megawatcher_internal_test.go juju-core-1.18.0/src/launchpad.net/juju-core/state/megawatcher_internal_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/state/megawatcher_internal_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/state/megawatcher_internal_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -420,6 +420,44 @@ StatusInfo: "another failure", }, }, + }, { + about: "unit addresses are read from the assigned machine for recent Juju releases", + setUp: func(c *gc.C, st *State) { + wordpress := AddTestingService(c, st, "wordpress", AddTestingCharm(c, st, "wordpress")) + u, err := wordpress.AddUnit() + c.Assert(err, gc.IsNil) + err = u.OpenPort("tcp", 12345) + c.Assert(err, gc.IsNil) + m, err := st.AddMachine("quantal", JobHostUnits) + c.Assert(err, gc.IsNil) + err = u.AssignToMachine(m) + c.Assert(err, gc.IsNil) + publicAddress := instance.NewAddress("public") + publicAddress.NetworkScope = instance.NetworkPublic + privateAddress := instance.NewAddress("private") + privateAddress.NetworkScope = instance.NetworkCloudLocal + err = m.SetAddresses([]instance.Address{publicAddress, privateAddress}) + c.Assert(err, gc.IsNil) + err = u.SetStatus(params.StatusError, "failure", nil) + c.Assert(err, gc.IsNil) + }, + change: watcher.Change{ + C: "units", + Id: "wordpress/0", + }, + expectContents: []params.EntityInfo{ + ¶ms.UnitInfo{ + Name: "wordpress/0", + Service: "wordpress", + Series: "quantal", + PublicAddress: "public", + PrivateAddress: "private", + MachineId: "0", + Ports: []instance.Port{{"tcp", 12345}}, + Status: params.StatusError, + StatusInfo: "failure", + }, + }, }, // Service changes { diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/state/unit.go juju-core-1.18.0/src/launchpad.net/juju-core/state/unit.go --- juju-core-1.17.7/src/launchpad.net/juju-core/state/unit.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/state/unit.go 2014-04-04 16:57:31.000000000 +0000 @@ -117,11 +117,11 @@ if err != nil { return nil, err } - charm, err := u.st.Charm(u.doc.CharmURL) + chrm, err := u.st.Charm(u.doc.CharmURL) if err != nil { return nil, err } - result := charm.Config().DefaultSettings() + result := chrm.Config().DefaultSettings() for name, value := range settings.Map() { result[name] = value } @@ -498,8 +498,10 @@ // addressesOfMachine returns Addresses of the related machine if present. func (u *Unit) addressesOfMachine() []instance.Address { - id := u.doc.MachineId - if id != "" { + if id, err := u.AssignedMachineId(); err != nil { + unitLogger.Errorf("unit %v cannot get assigned machine: %v", u, err) + return nil + } else { m, err := u.st.Machine(id) if err == nil { return m.Addresses() diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/state/unit_test.go juju-core-1.18.0/src/launchpad.net/juju-core/state/unit_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/state/unit_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/state/unit_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -169,6 +169,54 @@ c.Assert(err, gc.ErrorMatches, `cannot set public address of unit "wordpress/0": unit not found`) } +func (s *UnitSuite) addSubordinateUnit(c *gc.C) *state.Unit { + subCharm := s.AddTestingCharm(c, "logging") + s.AddTestingService(c, "logging", subCharm) + eps, err := s.State.InferEndpoints([]string{"wordpress", "logging"}) + c.Assert(err, gc.IsNil) + rel, err := s.State.AddRelation(eps...) + c.Assert(err, gc.IsNil) + ru, err := rel.Unit(s.unit) + c.Assert(err, gc.IsNil) + err = ru.EnterScope(nil) + c.Assert(err, gc.IsNil) + subUnit, err := s.State.Unit("logging/0") + c.Assert(err, gc.IsNil) + return subUnit +} + +func (s *UnitSuite) setAssignedMachineAddresses(c *gc.C, u *state.Unit) { + err := u.AssignToNewMachine() + c.Assert(err, gc.IsNil) + mid, err := u.AssignedMachineId() + c.Assert(err, gc.IsNil) + machine, err := s.State.Machine(mid) + c.Assert(err, gc.IsNil) + err = machine.SetProvisioned("i-exist", "fake_nonce", nil) + c.Assert(err, gc.IsNil) + err = machine.SetAddresses([]instance.Address{{ + Type: instance.Ipv4Address, + NetworkScope: instance.NetworkCloudLocal, + Value: "private.address.example.com", + }, { + Type: instance.Ipv4Address, + NetworkScope: instance.NetworkPublic, + Value: "public.address.example.com", + }}) + c.Assert(err, gc.IsNil) +} + +func (s *UnitSuite) TestGetPublicAddressSubordinate(c *gc.C) { + subUnit := s.addSubordinateUnit(c) + _, ok := subUnit.PublicAddress() + c.Assert(ok, gc.Equals, false) + + s.setAssignedMachineAddresses(c, s.unit) + address, ok := subUnit.PublicAddress() + c.Assert(ok, gc.Equals, true) + c.Assert(address, gc.Equals, "public.address.example.com") +} + func (s *UnitSuite) TestGetPublicAddressFromMachine(c *gc.C) { machine, err := s.State.AddMachine("quantal", state.JobHostUnits) c.Assert(err, gc.IsNil) @@ -208,6 +256,17 @@ c.Assert(err, gc.ErrorMatches, `cannot set private address of unit "wordpress/0": unit not found`) } +func (s *UnitSuite) TestGetPrivateAddressSubordinate(c *gc.C) { + subUnit := s.addSubordinateUnit(c) + _, ok := subUnit.PrivateAddress() + c.Assert(ok, gc.Equals, false) + + s.setAssignedMachineAddresses(c, s.unit) + address, ok := subUnit.PrivateAddress() + c.Assert(ok, gc.Equals, true) + c.Assert(address, gc.Equals, "private.address.example.com") +} + func (s *UnitSuite) TestGetPrivateAddressFromMachine(c *gc.C) { machine, err := s.State.AddMachine("quantal", state.JobHostUnits) c.Assert(err, gc.IsNil) @@ -619,18 +678,7 @@ func (s *UnitSuite) TestSetMongoPasswordOnUnitAfterConnectingAsMachineEntity(c *gc.C) { // Make a second unit to use later. (Subordinate units can only be created // as a side-effect of a principal entering relation scope.) - subCharm := s.AddTestingCharm(c, "logging") - s.AddTestingService(c, "logging", subCharm) - eps, err := s.State.InferEndpoints([]string{"wordpress", "logging"}) - c.Assert(err, gc.IsNil) - rel, err := s.State.AddRelation(eps...) - c.Assert(err, gc.IsNil) - ru, err := rel.Unit(s.unit) - c.Assert(err, gc.IsNil) - err = ru.EnterScope(nil) - c.Assert(err, gc.IsNil) - subUnit, err := s.State.Unit("logging/0") - c.Assert(err, gc.IsNil) + subUnit := s.addSubordinateUnit(c) info := state.TestingStateInfo() st, err := state.Open(info, state.TestingDialOpts(), state.Policy(nil)) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/testing/environ.go juju-core-1.18.0/src/launchpad.net/juju-core/testing/environ.go --- juju-core-1.17.7/src/launchpad.net/juju-core/testing/environ.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/testing/environ.go 2014-04-04 16:57:31.000000000 +0000 @@ -28,6 +28,8 @@ } } +const FakeDefaultSeries = "precise" + // FakeConfig() returns an environment configuration for a // fake provider with all required attributes set. func FakeConfig() Attrs { @@ -43,7 +45,7 @@ "development": false, "state-port": 19034, "api-port": 17777, - "default-series": config.DefaultSeries, + "default-series": FakeDefaultSeries, } } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/utils/ssh/authorisedkeys.go juju-core-1.18.0/src/launchpad.net/juju-core/utils/ssh/authorisedkeys.go --- juju-core-1.17.7/src/launchpad.net/juju-core/utils/ssh/authorisedkeys.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/utils/ssh/authorisedkeys.go 2014-04-04 16:53:35.000000000 +0000 @@ -264,15 +264,6 @@ existingNonKeyLines = append(existingNonKeyLines, line) } } - for _, newKey := range newKeys { - _, comment, err := KeyFingerprint(newKey) - if err != nil { - return err - } - if comment == "" { - return fmt.Errorf("cannot add ssh key without comment") - } - } return writeAuthorisedKeys(user, append(existingNonKeyLines, newKeys...)) } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/utils/ssh/authorisedkeys_test.go juju-core-1.18.0/src/launchpad.net/juju-core/utils/ssh/authorisedkeys_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/utils/ssh/authorisedkeys_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/utils/ssh/authorisedkeys_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -183,7 +183,12 @@ anotherKey := sshtesting.ValidKeyTwo.Key writeAuthKeysFile(c, []string{firstKey, anotherKey}) - replaceKey := sshtesting.ValidKeyThree.Key + " anotheruser@host" + // replaceKey is created without a comment so test that + // ReplaceKeys handles keys without comments. This is + // because existing keys may not have a comment and + // ReplaceKeys is used to rewrite the entire authorized_keys + // file when adding new keys. + replaceKey := sshtesting.ValidKeyThree.Key err := ssh.ReplaceKeys(testSSHUser, replaceKey) c.Assert(err, gc.IsNil) actual, err := ssh.ListKeys(testSSHUser, ssh.FullKeys) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/version/version.go juju-core-1.18.0/src/launchpad.net/juju-core/version/version.go --- juju-core-1.17.7/src/launchpad.net/juju-core/version/version.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/version/version.go 2014-04-04 16:57:31.000000000 +0000 @@ -23,7 +23,7 @@ // The presence and format of this constant is very important. // The debian/rules build recipe uses this value for the version // number of the release package. -const version = "1.17.7" +const version = "1.18.0" // lsbReleaseFile is the name of the file that is read in order to determine // the release version of ubuntu. diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/container_initialisation_test.go juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/container_initialisation_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/container_initialisation_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/container_initialisation_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -12,10 +12,10 @@ "launchpad.net/juju-core/agent" "launchpad.net/juju-core/environs" - "launchpad.net/juju-core/environs/config" "launchpad.net/juju-core/instance" "launchpad.net/juju-core/state" apiprovisioner "launchpad.net/juju-core/state/api/provisioner" + coretesting "launchpad.net/juju-core/testing" "launchpad.net/juju-core/utils" "launchpad.net/juju-core/version" "launchpad.net/juju-core/worker" @@ -88,7 +88,7 @@ // make a container on the host machine template := state.MachineTemplate{ - Series: config.DefaultSeries, + Series: coretesting.FakeDefaultSeries, Jobs: []state.MachineJob{state.JobHostUnits}, } container, err := s.State.AddMachineInsideMachine(template, host.Id(), ctype) @@ -131,7 +131,7 @@ for _, ctype := range instance.ContainerTypes { // create a machine to host the container. m, err := s.BackingState.AddOneMachine(state.MachineTemplate{ - Series: config.DefaultSeries, + Series: coretesting.FakeDefaultSeries, Jobs: []state.MachineJob{state.JobHostUnits}, Constraints: s.defaultConstraints, }) @@ -154,7 +154,7 @@ // create a machine to host the container. m, err := s.BackingState.AddOneMachine(state.MachineTemplate{ - Series: config.DefaultSeries, + Series: coretesting.FakeDefaultSeries, Jobs: []state.MachineJob{state.JobHostUnits}, Constraints: s.defaultConstraints, }) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/export_test.go juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/export_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/export_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/export_test.go 2014-04-04 16:53:35.000000000 +0000 @@ -5,6 +5,7 @@ import ( "launchpad.net/juju-core/environs/config" + "launchpad.net/juju-core/state/api/watcher" ) func SetObserver(p Provisioner, observer chan<- *config.Config) { @@ -13,3 +14,7 @@ ep.observer = observer ep.Unlock() } + +func GetRetryWatcher(p Provisioner) (watcher.NotifyWatcher, error) { + return p.getRetryWatcher() +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/kvm-broker_test.go juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/kvm-broker_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/kvm-broker_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/kvm-broker_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -16,7 +16,7 @@ "launchpad.net/juju-core/container/kvm/mock" kvmtesting "launchpad.net/juju-core/container/kvm/testing" "launchpad.net/juju-core/environs" - "launchpad.net/juju-core/environs/config" + "launchpad.net/juju-core/errors" "launchpad.net/juju-core/instance" instancetest "launchpad.net/juju-core/instance/testing" jujutesting "launchpad.net/juju-core/juju/testing" @@ -161,7 +161,7 @@ // The kvm provisioner actually needs the machine it is being created on // to be in state, in order to get the watcher. - m, err := s.State.AddMachine(config.DefaultSeries, state.JobHostUnits, state.JobManageEnviron) + m, err := s.State.AddMachine(coretesting.FakeDefaultSeries, state.JobHostUnits, state.JobManageEnviron) c.Assert(err, gc.IsNil) err = m.SetAddresses([]instance.Address{ instance.NewAddress("0.1.2.3"), @@ -228,15 +228,24 @@ defer stop(c, p) // Check that an instance is not provisioned when the machine is created. - _, err := s.State.AddMachine(config.DefaultSeries, state.JobHostUnits) + _, err := s.State.AddMachine(coretesting.FakeDefaultSeries, state.JobHostUnits) c.Assert(err, gc.IsNil) s.expectNoEvents(c) } +func (s *kvmProvisionerSuite) TestDoesNotHaveRetryWatcher(c *gc.C) { + p := s.newKvmProvisioner(c) + defer stop(c, p) + + w, err := provisioner.GetRetryWatcher(p) + c.Assert(w, gc.IsNil) + c.Assert(errors.IsNotImplementedError(err), jc.IsTrue) +} + func (s *kvmProvisionerSuite) addContainer(c *gc.C) *state.Machine { template := state.MachineTemplate{ - Series: config.DefaultSeries, + Series: coretesting.FakeDefaultSeries, Jobs: []state.MachineJob{state.JobHostUnits}, } container, err := s.State.AddMachineInsideMachine(template, s.machineId, instance.KVM) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/lxc-broker_test.go juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/lxc-broker_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/lxc-broker_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/lxc-broker_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -17,7 +17,7 @@ "launchpad.net/juju-core/container/lxc/mock" lxctesting "launchpad.net/juju-core/container/lxc/testing" "launchpad.net/juju-core/environs" - "launchpad.net/juju-core/environs/config" + "launchpad.net/juju-core/errors" "launchpad.net/juju-core/instance" instancetest "launchpad.net/juju-core/instance/testing" jujutesting "launchpad.net/juju-core/juju/testing" @@ -190,7 +190,7 @@ // The lxc provisioner actually needs the machine it is being created on // to be in state, in order to get the watcher. - m, err := s.State.AddMachine(config.DefaultSeries, state.JobHostUnits, state.JobManageEnviron) + m, err := s.State.AddMachine(coretesting.FakeDefaultSeries, state.JobHostUnits, state.JobManageEnviron) c.Assert(err, gc.IsNil) err = m.SetAddresses([]instance.Address{ instance.NewAddress("0.1.2.3"), @@ -261,15 +261,24 @@ defer stop(c, p) // Check that an instance is not provisioned when the machine is created. - _, err := s.State.AddMachine(config.DefaultSeries, state.JobHostUnits) + _, err := s.State.AddMachine(coretesting.FakeDefaultSeries, state.JobHostUnits) c.Assert(err, gc.IsNil) s.expectNoEvents(c) } +func (s *lxcProvisionerSuite) TestDoesNotHaveRetryWatcher(c *gc.C) { + p := s.newLxcProvisioner(c) + defer stop(c, p) + + w, err := provisioner.GetRetryWatcher(p) + c.Assert(w, gc.IsNil) + c.Assert(errors.IsNotImplementedError(err), jc.IsTrue) +} + func (s *lxcProvisionerSuite) addContainer(c *gc.C) *state.Machine { template := state.MachineTemplate{ - Series: config.DefaultSeries, + Series: coretesting.FakeDefaultSeries, Jobs: []state.MachineJob{state.JobHostUnits}, } container, err := s.State.AddMachineInsideMachine(template, s.parentMachineId, instance.LXC) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/provisioner.go juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/provisioner.go --- juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/provisioner.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/provisioner.go 2014-04-04 16:57:31.000000000 +0000 @@ -12,6 +12,7 @@ "launchpad.net/juju-core/agent" "launchpad.net/juju-core/environs" "launchpad.net/juju-core/environs/config" + "launchpad.net/juju-core/errors" "launchpad.net/juju-core/instance" apiprovisioner "launchpad.net/juju-core/state/api/provisioner" apiwatcher "launchpad.net/juju-core/state/api/watcher" @@ -30,6 +31,7 @@ worker.Worker Stop() error getMachineWatcher() (apiwatcher.StringsWatcher, error) + getRetryWatcher() (apiwatcher.NotifyWatcher, error) } // environProvisioner represents a running provisioning worker for machine nodes @@ -108,8 +110,8 @@ if err != nil { return nil, err } - retryWatcher, err := p.st.WatchMachineErrorRetry() - if err != nil { + retryWatcher, err := p.getRetryWatcher() + if err != nil && !errors.IsNotImplementedError(err) { return nil, err } task := NewProvisionerTask( @@ -193,6 +195,10 @@ return p.st.WatchEnvironMachines() } +func (p *environProvisioner) getRetryWatcher() (apiwatcher.NotifyWatcher, error) { + return p.st.WatchMachineErrorRetry() +} + // setConfig updates the environment configuration and notifies // the config observer. func (p *environProvisioner) setConfig(environConfig *config.Config) error { @@ -263,3 +269,7 @@ } return machine.WatchContainers(p.containerType) } + +func (p *containerProvisioner) getRetryWatcher() (apiwatcher.NotifyWatcher, error) { + return nil, errors.NewNotImplementedError("getRetryWatcher") +} diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/provisioner_task.go juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/provisioner_task.go --- juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/provisioner_task.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/provisioner_task.go 2014-04-04 16:57:31.000000000 +0000 @@ -119,6 +119,12 @@ // see all legitimate instances as unknown. var safeModeChan chan bool + // Not all provisioners have a retry channel. + var retryChan <-chan struct{} + if task.retryWatcher != nil { + retryChan = task.retryWatcher.Changes() + } + // When the watcher is started, it will have the initial changes be all // the machines that are relevant. Also, since this is available straight // away, we know there will be some changes right off the bat. @@ -149,7 +155,7 @@ return fmt.Errorf("failed to process machines after safe mode disabled: %v", err) } } - case <-task.retryWatcher.Changes(): + case <-retryChan: if err := task.processMachinesWithTransientErrors(); err != nil { return fmt.Errorf("failed to process machines with transient errors: %v", err) } diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/provisioner_test.go juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/provisioner_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/worker/provisioner/provisioner_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/worker/provisioner/provisioner_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -355,7 +355,7 @@ func (s *ProvisionerSuite) addMachine() (*state.Machine, error) { return s.BackingState.AddOneMachine(state.MachineTemplate{ - Series: config.DefaultSeries, + Series: coretesting.FakeDefaultSeries, Jobs: []state.MachineJob{state.JobHostUnits}, Constraints: s.defaultConstraints, }) @@ -436,7 +436,7 @@ // make a container on the machine we just created template := state.MachineTemplate{ - Series: config.DefaultSeries, + Series: coretesting.FakeDefaultSeries, Jobs: []state.MachineJob{state.JobHostUnits}, } container, err := s.State.AddMachineInsideMachine(template, m.Id(), instance.LXC) diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/worker/uniter/modes.go juju-core-1.18.0/src/launchpad.net/juju-core/worker/uniter/modes.go --- juju-core-1.17.7/src/launchpad.net/juju-core/worker/uniter/modes.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/worker/uniter/modes.go 2014-04-04 16:53:35.000000000 +0000 @@ -11,7 +11,6 @@ "launchpad.net/juju-core/charm" "launchpad.net/juju-core/charm/hooks" - "launchpad.net/juju-core/environs" "launchpad.net/juju-core/state/api/params" "launchpad.net/juju-core/state/watcher" "launchpad.net/juju-core/worker" @@ -26,31 +25,6 @@ // ModeInit is the initial Uniter mode. func ModeInit(u *Uniter) (next Mode, err error) { defer modeContext("ModeInit", &err)() - logger.Infof("updating unit addresses") - // TODO(dimitern): We might be able to drop all this address stuff - // entirely once we have machine addresses. - providerType, err := u.st.ProviderType() - if err != nil { - return nil, err - } - provider, err := environs.Provider(providerType) - if err != nil { - return nil, err - } - if private, err := provider.PrivateAddress(); err != nil { - logger.Errorf("cannot get unit's private address: %v", err) - return nil, err - } else if err = u.unit.SetPrivateAddress(private); err != nil { - logger.Errorf("cannot set unit's private address: %v", err) - return nil, err - } - if public, err := provider.PublicAddress(); err != nil { - logger.Errorf("cannot get unit's public address: %v", err) - return nil, err - } else if err = u.unit.SetPublicAddress(public); err != nil { - logger.Errorf("cannot set unit's public address: %v", err) - return nil, err - } logger.Infof("reconciling relation state") if err := u.restoreRelations(); err != nil { return nil, err diff -Nru juju-core-1.17.7/src/launchpad.net/juju-core/worker/uniter/uniter_test.go juju-core-1.18.0/src/launchpad.net/juju-core/worker/uniter/uniter_test.go --- juju-core-1.17.7/src/launchpad.net/juju-core/worker/uniter/uniter_test.go 2014-03-27 15:48:42.000000000 +0000 +++ juju-core-1.18.0/src/launchpad.net/juju-core/worker/uniter/uniter_test.go 2014-04-04 16:57:31.000000000 +0000 @@ -24,6 +24,7 @@ "launchpad.net/juju-core/agent/tools" "launchpad.net/juju-core/charm" "launchpad.net/juju-core/errors" + "launchpad.net/juju-core/instance" "launchpad.net/juju-core/juju/osenv" "launchpad.net/juju-core/juju/testing" "launchpad.net/juju-core/state" @@ -868,7 +869,7 @@ }, verifyFile{ testFile("jujuc.output"), - "user-admin\nprivate.dummy.address.example.com\npublic.dummy.address.example.com\n", + "user-admin\nprivate.address.example.com\npublic.address.example.com\n", }, ), ut( "run commands: proxy settings set", @@ -1087,6 +1088,28 @@ } } +// Assign the unit to a provisioned machine with dummy addresses set. +func assertAssignUnit(c *gc.C, st *state.State, u *state.Unit) { + err := u.AssignToNewMachine() + c.Assert(err, gc.IsNil) + mid, err := u.AssignedMachineId() + c.Assert(err, gc.IsNil) + machine, err := st.Machine(mid) + c.Assert(err, gc.IsNil) + err = machine.SetProvisioned("i-exist", "fake_nonce", nil) + c.Assert(err, gc.IsNil) + err = machine.SetAddresses([]instance.Address{{ + Type: instance.Ipv4Address, + NetworkScope: instance.NetworkCloudLocal, + Value: "private.address.example.com", + }, { + Type: instance.Ipv4Address, + NetworkScope: instance.NetworkPublic, + Value: "public.address.example.com", + }}) + c.Assert(err, gc.IsNil) +} + func (s *UniterSuite) TestSubordinateDying(c *gc.C) { // Create a test context for later use. ctx := &context{ @@ -1115,6 +1138,7 @@ c.Assert(err, gc.IsNil) rel, err := s.State.AddRelation(eps...) c.Assert(err, gc.IsNil) + assertAssignUnit(c, s.State, wpu) // Create the subordinate unit by entering scope as the principal. wpru, err := rel.Unit(wpu) @@ -1231,14 +1255,7 @@ c.Assert(err, gc.IsNil) // Assign the unit to a provisioned machine to match expected state. - err = unit.AssignToNewMachine() - c.Assert(err, gc.IsNil) - mid, err := unit.AssignedMachineId() - c.Assert(err, gc.IsNil) - machine, err := ctx.st.Machine(mid) - c.Assert(err, gc.IsNil) - err = machine.SetProvisioned("i-exist", "fake_nonce", nil) - c.Assert(err, gc.IsNil) + assertAssignUnit(c, ctx.st, unit) ctx.svc = svc ctx.unit = unit @@ -1270,11 +1287,11 @@ // GZ 2013-07-10: Hardcoded values from dummy environ // special cased here, questionable. private, _ := ctx.unit.PrivateAddress() - if private != "private.dummy.address.example.com" { + if private != "private.address.example.com" { continue } public, _ := ctx.unit.PublicAddress() - if public != "public.dummy.address.example.com" { + if public != "public.address.example.com" { continue } return