Chapter 09/circuit-oneops-1-master/components/cookbooks/netscaler/recipes/add_server.rb
#
# Cookbook Name:: netscaler
# Recipe:: add_server
#
# Copyright 2016, Walmart Stores, Inc.
#
# 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.
Cookbook Name:: netscaler
Recipe:: add_server



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



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.
Cookbook Name :: netscaler Recipe :: add_server 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 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 .
def new_server (server_key,request)
    resp_obj = JSON.parse(node.ns_conn.request(
      :method => :post, 
      :path =>"/nitro/v1/config/server", 
      :body => URI::encode(request)).body)
    if resp_obj["errorcode"] != 0
      if resp_obj["errorcode"] == 1335
        if resp_obj["message"].include?(server_key)
          Chef::Log.info( "server exists: #{resp_obj.inspect}")
        else
          if resp_obj["message"] =~ /exists \[(.*)\]/
            orphan = $1
            Chef::Log.info("cleaning up orphan: #{orphan}")
            del_server(orphan)
            sleep 1
            new_server(server_key,request)
          end
        end          
      else        
        Chef::Log.error( "post #{server_key} resp: #{resp_obj.inspect}")    
        exit 1      
      end        
    else
      Chef::Log.info( "post #{server_key} resp: #{resp_obj.inspect}")
    end  
end
def new_server ( server_key , request ) resp_obj = JSON . parse ( node . ns_conn . request ( :method => :post , :path => "/nitro/v1/config/server" , :body => URI :: encode ( request ) ) . body ) if resp_obj [ "errorcode" ] != 0 if resp_obj [ "errorcode" ] == 1335 if resp_obj [ "message" ] . include? ( server_key ) Chef :: Log . info ( "server exists: #{resp_obj.inspect}" ) else if resp_obj [ "message" ] =~ / exists  \[ (.*) \] / orphan = $1 Chef :: Log . info ( "cleaning up orphan: #{orphan}" ) del_server ( orphan ) sleep 1 new_server ( server_key , request ) end end else Chef :: Log . error ( "post #{server_key} resp: #{resp_obj.inspect}" ) exit 1 end else Chef :: Log . info ( "post #{server_key} resp: #{resp_obj.inspect}" ) end end
Cookbook Name:: netscaler
Recipe:: add_server
Cookbook Name :: netscaler Recipe :: add_server
#{"errorcode"=>1335, "message"=>"Server already exists [PyZDpVu1-5757594-2-10950000]"}
server_key
<not_specific>
request
<not_specific>