-
Notifications
You must be signed in to change notification settings - Fork 24
Start adding Orchestration API #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rkday
wants to merge
1
commit into
dtantsur:master
Choose a base branch
from
rkday:orchestration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2018 Dmitry Tantsur <divius.inside@gmail.com> | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| extern crate env_logger; | ||
| extern crate openstack; | ||
| extern crate waiter; | ||
|
|
||
| use std::env; | ||
| use waiter::{Waiter, WaiterCurrentState}; | ||
|
|
||
| #[cfg(feature = "orchestration")] | ||
| fn main() { | ||
| env_logger::init(); | ||
|
|
||
| let os = openstack::Cloud::from_env() | ||
| .expect("Failed to create an identity provider from the environment"); | ||
|
|
||
| let stackf = env::args().nth(1).expect("Provide a stack file"); | ||
| let envf = env::args().nth(2).expect("Provide an env file"); | ||
|
|
||
| let waiter = os | ||
| .new_stack(stackf, envf) | ||
| .create() | ||
| .expect("Cannot create a server"); | ||
| { | ||
| let current = waiter.waiter_current_state(); | ||
| println!( | ||
| "ID = {}, Name = {}, Status = {:?}", | ||
| current.id(), | ||
| current.name(), | ||
| current.status(), | ||
| ); | ||
| } | ||
|
|
||
| let server = waiter.wait().expect("Stack did not reach COMPLETE"); | ||
| println!( | ||
| "ID = {}, Name = {}, Status = {:?}", | ||
| server.id(), | ||
| server.name(), | ||
| server.status(), | ||
| ); | ||
| } | ||
|
|
||
| #[cfg(not(feature = "orchestration"))] | ||
| fn main() { | ||
| panic!("This example cannot run with 'orchestration' feature disabled"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| // Copyright 2017 Dmitry Tantsur <divius.inside@gmail.com> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar re copyrights |
||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Stack management via Orchestration API. | ||
| use std::rc::Rc; | ||
| use std::time::Duration; | ||
|
|
||
| use chrono::{DateTime, FixedOffset}; | ||
| use waiter::{Waiter, WaiterCurrentState}; | ||
| use super::{Error, ErrorKind, Result}; | ||
|
|
||
| mod protocol; | ||
| //mod api; | ||
|
|
||
| use super::common::{ | ||
| DeletionWaiter, | ||
| Refresh, | ||
| }; | ||
| use super::session::Session; | ||
|
|
||
| /// A request to create a stack. | ||
| #[derive(Debug)] | ||
| pub struct NewStack { | ||
| } | ||
|
|
||
| /// Structure representing a single stack. | ||
| #[derive(Clone, Debug)] | ||
| pub struct Stack { | ||
| session: Rc<Session>, | ||
| inner: protocol::Stack, | ||
| } | ||
|
|
||
| impl NewStack { | ||
| /// Start creating a server. | ||
| pub(crate) fn new(session: Rc<Session>, name: String) -> NewStack { | ||
| NewServer { | ||
| // session, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, you surely need it on line 60 |
||
| } | ||
| } | ||
|
|
||
| /// Request creation of the server. | ||
| pub fn create(self) -> Result<StackCreationWaiter> { | ||
| let request = protocol::StackCreate { | ||
| name: self.name, | ||
| }; | ||
|
|
||
| let stack_ref = api::create_stack(&self.session, request)?; | ||
| Ok(StackCreationWaiter { | ||
| stack: Stack::load(self.session, stack_ref.id)?, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// Waiter for server to be created. | ||
| #[derive(Debug)] | ||
| pub struct StackCreationWaiter { | ||
| stack: Stack, | ||
| } | ||
|
|
||
| /// Waiter for stack status to change. | ||
| #[derive(Debug)] | ||
| pub struct StackStatusWaiter<'stack> { | ||
| stack: &'stack mut Stack, | ||
| target: protocol::StackStatus, | ||
| } | ||
|
|
||
| impl Refresh for Stack { | ||
| /// Refresh the stack. | ||
| fn refresh(&mut self) -> Result<()> { | ||
| //self.inner = api::get_stack_by_id(&self.session, &self.inner.id)?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| impl Stack { | ||
| /// Create a new Stack object. | ||
| pub(crate) fn new(session: Rc<Session>, inner: protocol::Stack) -> Result<Stack> { | ||
| Ok(Stack { | ||
| session, | ||
| inner, | ||
| }) | ||
| } | ||
|
|
||
| /// Load a Stack object. | ||
| pub(crate) fn load<Id: AsRef<str>>(session: Rc<Session>, id: Id) -> Result<Stack> { | ||
| let inner = api::get_stack(&session, id)?; | ||
| Stack::new(session, inner) | ||
| } | ||
|
|
||
| transparent_property! { | ||
| #[doc = "Creation date and time."] | ||
| created_at: DateTime<FixedOffset> | ||
| } | ||
|
|
||
| transparent_property! { | ||
| #[doc = "Stack description."] | ||
| description: ref Option<String> | ||
| } | ||
|
|
||
| transparent_property! { | ||
| #[doc = "Stack unique ID."] | ||
| id: ref String | ||
| } | ||
|
|
||
| transparent_property! { | ||
| #[doc = "Stack name."] | ||
| name: ref String | ||
| } | ||
|
|
||
| transparent_property! { | ||
| #[doc = "Stack status."] | ||
| status: protocol::StackStatus | ||
| } | ||
|
|
||
| /// Delete the stack. | ||
| pub fn delete(self) -> Result<DeletionWaiter<Stack>> { | ||
| api::delete_stack(&self.session, &self.inner.id)?; | ||
| Ok(DeletionWaiter::new( | ||
| self, | ||
| Duration::new(120, 0), | ||
| Duration::new(1, 0), | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| impl<'server> Waiter<(), Error> for StackStatusWaiter<'server> { | ||
| fn default_wait_timeout(&self) -> Option<Duration> { | ||
| // TODO(dtantsur): vary depending on target? | ||
| Some(Duration::new(600, 0)) | ||
| } | ||
|
|
||
| fn default_delay(&self) -> Duration { | ||
| Duration::new(1, 0) | ||
| } | ||
|
|
||
| fn timeout_error(&self) -> Error { | ||
| Error::new( | ||
| ErrorKind::OperationTimedOut, | ||
| format!( | ||
| "Timeout waiting for stack {} to reach state {}", | ||
| self.stack.id(), | ||
| self.target | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| fn poll(&mut self) -> Result<Option<()>> { | ||
| self.stack.refresh()?; | ||
| if self.stack.status() == self.target { | ||
| debug!("Stack {} reached state {}", self.stack.id(), self.target); | ||
| Ok(Some(())) | ||
| } else if self.stack.status() == protocol::StackStatus::Failed { | ||
| debug!( | ||
| "Failed to move stack {} to {} - status is ERROR", | ||
| self.stack.id(), | ||
| self.target | ||
| ); | ||
| Err(Error::new( | ||
| ErrorKind::OperationFailed, | ||
| format!("Stack {} got into ERROR state", self.stack.id()), | ||
| )) | ||
| } else { | ||
| trace!( | ||
| "Still waiting for stack {} to get to state {}, current is {}", | ||
| self.stack.id(), | ||
| self.target, | ||
| self.stack.status() | ||
| ); | ||
| Ok(None) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'server> WaiterCurrentState<Stack> for StackStatusWaiter<'server> { | ||
| fn waiter_current_state(&self) -> &Stack { | ||
| &self.stack | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright 2017 Dmitry Tantsur <divius.inside@gmail.com> | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! JSON structures and protocol bits for the Orchestration API. | ||
|
|
||
| #![allow(non_snake_case)] | ||
| #![allow(missing_docs)] | ||
|
|
||
| use chrono::{DateTime, FixedOffset}; | ||
| use osproto::common::empty_as_default; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
|
|
||
| protocol_enum! { | ||
| #[doc = "Possible server statuses."] | ||
| enum StackStatus { | ||
| Complete = "CREATE_COMPLETE", | ||
| Failed = "CREATE_COMPLETE", | ||
| InProgress = "CREATE_IN_PROGRESS", | ||
| Unknown = "UNKNOWN" | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Deserialize)] | ||
| pub struct Stack { | ||
| #[serde(rename = "created")] | ||
| pub created_at: DateTime<FixedOffset>, | ||
| #[serde(deserialize_with = "empty_as_default", default)] | ||
| pub description: Option<String>, | ||
| pub id: String, | ||
| pub name: String, | ||
| pub status: StackStatus, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Serialize)] | ||
| pub struct StackCreate { | ||
| pub name: String, | ||
| } | ||
|
|
||
| impl Default for StackStatus { | ||
| fn default() -> StackStatus { | ||
| StackStatus::Unknown | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put your copyright here or just skip the copyright completely.