components/ruby/puppet-modules/oracle-solaris_providers/files/etc/puppet/modules/solaris_providers/lib/puppet/provider/evs/solaris.rb
changeset 5438 c068f8c677e8
parent 4801 c249904bb056
equal deleted inserted replaced
5437:449f3459d285 5438:c068f8c677e8
       
     1 #
       
     2 # CDDL HEADER START
       
     3 #
       
     4 # The contents of this file are subject to the terms of the
       
     5 # Common Development and Distribution License (the "License").
       
     6 # You may not use this file except in compliance with the License.
       
     7 #
       
     8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9 # or http://www.opensolaris.org/os/licensing.
       
    10 # See the License for the specific language governing permissions
       
    11 # and limitations under the License.
       
    12 #
       
    13 # When distributing Covered Code, include this CDDL HEADER in each
       
    14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15 # If applicable, add the following below this CDDL HEADER, with the
       
    16 # fields enclosed by brackets "[]" replaced with your own identifying
       
    17 # information: Portions Copyright [yyyy] [name of copyright owner]
       
    18 #
       
    19 # CDDL HEADER END
       
    20 #
       
    21 
       
    22 #
       
    23 # Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
    24 #
       
    25 
       
    26 Puppet::Type.type(:evs).provide(:evs) do
       
    27     desc "Provider for managing EVS setup in the Solaris OS"
       
    28     confine :operatingsystem => [:solaris]
       
    29     defaultfor :osfamily => :solaris, :kernelrelease => ["5.11", "5.12"]
       
    30     commands :evsadm => "/usr/sbin/evsadm"
       
    31 
       
    32     mk_resource_methods
       
    33 
       
    34     def initialize(value={})
       
    35         super(value)
       
    36         @property_flush = {}
       
    37     end
       
    38 
       
    39     def self.get_evs_list
       
    40         begin
       
    41             evs_list = evsadm("show-evs", "-c", "-o", "evs,tenant,status")
       
    42                 .split("\n")
       
    43         rescue Puppet::ExecutionFailure => e
       
    44             raise Puppet::Error, "Unable to populate EVS instances: \n" \
       
    45                 "#{e.inspect}"
       
    46         end
       
    47         evs_list
       
    48     end
       
    49 
       
    50     def self.get_evs_properties(evs, tenant, status, ensure_val)
       
    51         evs_properties = {}
       
    52         evs_fullname = tenant + "/" + evs
       
    53 
       
    54         evs_properties[:name] = evs_fullname
       
    55         evs_properties[:status] = status 
       
    56         evs_properties[:ensure] = ensure_val
       
    57         
       
    58         evsadm("show-evsprop", "-f", "tenant=#{tenant}", "-c", "-o", 
       
    59             "property,value", evs).split("\n").collect do |each_evsprop|
       
    60             property, value = each_evsprop.split(":")
       
    61             value = "" if value.nil?
       
    62             case property
       
    63             # read/write properties (always updatable)
       
    64             when "maxbw"
       
    65                 evs_properties[:maxbw] = value
       
    66             when "priority"
       
    67                 evs_properties[:priority] = value
       
    68             when "protection"
       
    69                 evs_properties[:protection] = value
       
    70             # read-only properties (settable upon creation)
       
    71             when "l2-type"
       
    72                 evs_properties[:l2_type] = value
       
    73             when "vlanid"
       
    74                 evs_properties[:vlanid] = value
       
    75             when "vni"
       
    76                 evs_properties[:vni] = value
       
    77             when "uuid"
       
    78                 evs_properties[:uuid] = value
       
    79             end
       
    80         end
       
    81         
       
    82         Puppet.debug "EVS Properties: #{evs_properties.inspect}"
       
    83         evs_properties
       
    84     end
       
    85     
       
    86     def self.instances
       
    87         get_evs_list.collect do |each_evs|
       
    88             evs, tenant, status = each_evs.strip.split(":")
       
    89             evs_properties = get_evs_properties(evs, tenant, status, :present)
       
    90             new(evs_properties) # Create a provider instance
       
    91         end
       
    92     end
       
    93 
       
    94     def self.prefetch(resources)
       
    95         instances.each do |inst|
       
    96             if resource = resources[inst.name]
       
    97                 resource.provider = inst
       
    98             end
       
    99         end
       
   100     end
       
   101     
       
   102     def exists?
       
   103         @property_hash[:ensure] == :present
       
   104     end
       
   105 
       
   106     def create
       
   107         tenant, evs = get_tenant_and_evs_name
       
   108         begin
       
   109             create_evs(tenant, evs, add_properties(@resource))
       
   110         rescue Puppet::ExecutionFailure => e
       
   111             raise Puppet::Error, "Cannot create EVS: \n#{e.inspect}"
       
   112         end
       
   113     end
       
   114 
       
   115     def destroy
       
   116         tenant, evs = get_tenant_and_evs_name
       
   117         begin
       
   118             delete_evs(tenant, evs)
       
   119         rescue Puppet::ExecutionFailure => e
       
   120             raise Puppet::Error, "Cannot delete EVS: \n#{e.inspect}"
       
   121         end
       
   122     end
       
   123 
       
   124     ### Define Setters ###
       
   125     ## read/write properties (always updatable) ##
       
   126     def maxbw=(value)
       
   127         @property_flush[:maxbw] = value
       
   128     end
       
   129 
       
   130     def priority=(value)
       
   131         @property_flush[:priority] = value
       
   132     end
       
   133     
       
   134     def protection=(value)
       
   135         @property_flush[:protection] = value
       
   136     end
       
   137     
       
   138     ## read-only properties (settable upon creation) ##
       
   139     def l2_type=(value)
       
   140         raise Puppet::Error, "l2_type property is settable only upon creation"
       
   141     end
       
   142     
       
   143     def vlanid=(value)
       
   144         raise Puppet::Error, "valid property is settable only upon creation"
       
   145     end
       
   146     
       
   147     def vni=(value)
       
   148         raise Puppet::Error, "vni property is settable only upon creation"
       
   149     end
       
   150     
       
   151     def uuid=(value)
       
   152         raise Puppet::Error, "uuid property is settable only upon creation"
       
   153     end
       
   154    
       
   155     # Create EVS instance
       
   156     def create_evs(tenant, evs, properties)
       
   157         begin
       
   158             evsadm("create-evs", "-T", tenant, properties, evs)
       
   159         rescue Puppet::ExecutionFailure => e
       
   160             # Pass up the exception to upper level
       
   161             raise
       
   162         end
       
   163     end
       
   164 
       
   165     # Destroy EVS instance
       
   166     def delete_evs(tenant, evs)
       
   167         begin
       
   168             evsadm("delete-evs", "-T", tenant, evs)
       
   169         rescue Puppet::ExecutionFailure => e
       
   170             # Pass up the exception to upper level
       
   171             raise
       
   172         end
       
   173     end
       
   174     
       
   175     # Set read/write property of EVS instance
       
   176     def set_evsprop(tenant, evs, property)
       
   177         begin
       
   178             evsadm("set-evsprop", "-T", tenant, property, evs)
       
   179         rescue Puppet::ExecutionFailure => e
       
   180             # Pass up the exception to upper level
       
   181             raise
       
   182         end
       
   183     end
       
   184 
       
   185     # Parse the "name" value from user and yield tenant and EVS instance name
       
   186     def get_tenant_and_evs_name()
       
   187         usrstr = @resource[:name].split("/")
       
   188         if usrstr.length == 2
       
   189             return usrstr[0], usrstr[1]
       
   190         else
       
   191             raise Puppet::Error, "Invalid EVS name #{@resource[:name]} \n" \
       
   192                 "Name convention must be <tenant>/<evs>"
       
   193         end 
       
   194     end
       
   195     
       
   196     # property setter for EVS creation
       
   197     def add_properties(source)
       
   198         p = []
       
   199         prop_list = {
       
   200             "maxbw" => source[:maxbw], 
       
   201             "priority" => source[:priority],
       
   202             "protection" => source[:protection],
       
   203             "l2-type" => source[:l2_type],
       
   204             "vlanid" => source[:vlanid],
       
   205             "vni" => source[:vni],
       
   206             "uuid" => source[:uuid]
       
   207             }
       
   208         prop_list.each do |key, value|
       
   209             next if (value == nil) || (value == "")
       
   210             p << "#{key}=#{value}"
       
   211         end
       
   212         return [] if p.empty?
       
   213         properties = Array["-p", p.join(",")]
       
   214     end
       
   215 
       
   216     # Update property change
       
   217     def flush
       
   218         tenant, evs = get_tenant_and_evs_name
       
   219 
       
   220         # Update property values when specified
       
   221         unless @property_flush.empty?
       
   222             # update multiple property values iteratively
       
   223             @property_flush.each do |key, value|
       
   224                 prop = ["-p", "#{key}=#{value}"]
       
   225                 begin 
       
   226                     set_evsprop(tenant, evs, prop)
       
   227                 rescue Puppet::ExecutionFailure => e
       
   228                     raise Puppet::Error, "Cannot update the property " \
       
   229                         "#{key}=#{value}.\n#{e.inspect}"
       
   230                 end
       
   231             end
       
   232         end
       
   233         # Synchronize all the SHOULD values to IS values
       
   234         @property_hash = resource.to_hash
       
   235     end
       
   236 end