components/ruby/puppet-modules/oracle-solaris_providers/files/etc/puppet/modules/solaris_providers/lib/puppet/provider/evs_vport/solaris.rb
changeset 5438 c068f8c677e8
parent 4794 be62c55aa235
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 
       
    27 Puppet::Type.type(:evs_vport).provide(:evs_vport) do
       
    28     desc "Provider for managing EVS VPort setup in the Solaris OS"
       
    29     confine :operatingsystem => [:solaris]
       
    30     defaultfor :osfamily => :solaris, :kernelrelease => ["5.11", "5.12"]
       
    31     commands :evsadm => "/usr/sbin/evsadm"
       
    32 
       
    33     mk_resource_methods
       
    34 
       
    35     def initialize(value={})
       
    36         super(value)
       
    37         @property_flush = {}
       
    38     end
       
    39 
       
    40     def self.get_vport_list
       
    41         begin
       
    42             vport_list = evsadm("show-vport", "-c", "-o", 
       
    43                 "name,tenant,status").split("\n")
       
    44         rescue Puppet::ExecutionFailure => e
       
    45             raise Puppet::Error, "unable to populate VPort instances: \n" \
       
    46                 "#{e.inspect}"
       
    47             return nil
       
    48         end
       
    49         vport_list
       
    50     end
       
    51    
       
    52     def self.get_vport_properties(vport, tenant, status, ensure_val)
       
    53         vport_props = {}
       
    54         vport_fullname = tenant + "/" + vport
       
    55         vport_props[:name] = vport_fullname
       
    56         vport_props[:status] = status
       
    57         vport_props[:ensure] = ensure_val
       
    58         
       
    59         evsadm("show-vportprop", "-f", "tenant=#{tenant}", "-c", "-o",
       
    60             "property,value", vport).split("\n").collect do |each_prop|
       
    61             property, value = each_prop.split(":", 2)
       
    62             value = "" if value.nil?
       
    63             case property
       
    64             # read/write properties (always updatable)
       
    65             when "cos"
       
    66                 vport_props[:cos] = value
       
    67             when "maxbw"
       
    68                 vport_props[:maxbw] = value
       
    69             when "priority"
       
    70                 vport_props[:priority] = value
       
    71             when "protection"
       
    72                 vport_props[:protection] = value
       
    73             # read-only properties (settable upon creation)
       
    74             when "ipaddr"
       
    75                 vport_props[:ipaddr] = value
       
    76             when "macaddr"
       
    77                 # macaddr ":" appears to be "\:". change it to ":"
       
    78                 vport_props[:macaddr] = value.gsub! "\\:", ":"
       
    79             when "uuid"
       
    80                 vport_props[:uuid] = value
       
    81             end
       
    82         end
       
    83 
       
    84         Puppet.debug "VPort properties: #{vport_props.inspect}"
       
    85         vport_props
       
    86     end
       
    87 
       
    88     def self.instances
       
    89         get_vport_list.collect do |each_vport|
       
    90             vport, tenant, status = each_vport.strip.split(":")
       
    91             vport_props = get_vport_properties(vport, tenant, status, :present)
       
    92             new(vport_props) # Create a provider instance
       
    93         end
       
    94     end        
       
    95         
       
    96     def self.prefetch(resources)
       
    97         instances.each do |inst|
       
    98             if resource = resources[inst.name]
       
    99                 resource.provider = inst
       
   100             end
       
   101         end
       
   102     end
       
   103 
       
   104     def exists?
       
   105         @property_hash[:ensure] == :present
       
   106     end
       
   107 
       
   108     def create
       
   109         tenant, vport = get_tenant_and_vport_name
       
   110         begin
       
   111             create_vport(tenant, vport, add_properties(@resource))
       
   112         rescue Puppet::ExecutionFailure => e
       
   113             raise Puppet::Error, "Cannot add VPort: \n #{e.inspect}"
       
   114         end
       
   115     end
       
   116 
       
   117     def destroy
       
   118         tenant, vport = get_tenant_and_vport_name
       
   119         begin
       
   120             delete_vport(tenant, vport)
       
   121         rescue Puppet::ExecutionFailure => e
       
   122             raise Puppet::Error, "Cannot remove VPort: \n #{e.inspect}"
       
   123         end
       
   124     end
       
   125 
       
   126     def reset
       
   127         tenant, vport = get_tenant_and_vport_name
       
   128         begin
       
   129             evsadm("reset-vport", "-T", tenant, vport)
       
   130         rescue Puppet::ExecutionFailure => e
       
   131             raise Puppet::Error, "Cannot reset VPort: \n #{e.inspect}"
       
   132         end
       
   133 
       
   134         @resource[:ensure] = :present
       
   135         Puppet::notice "The VPort has been successfully reset."
       
   136     end
       
   137     
       
   138     ### Define Setters ###
       
   139     ## read/write properties (always updatable) ##
       
   140     def cos=(value)
       
   141         @property_flush[:cos] = value
       
   142     end
       
   143 
       
   144     def maxbw=(value)
       
   145         @property_flush[:maxbw] = value
       
   146     end
       
   147     
       
   148     def priority=(value)
       
   149         @property_flush[:priority] = value
       
   150     end
       
   151     
       
   152     def protection=(value)
       
   153         @property_flush[:protection] = value
       
   154     end
       
   155 
       
   156     ## read-only properties (settable upon creation) ##
       
   157     def ipaddr=(value)
       
   158         raise Puppet::Error, "ipaddr property is settable only upon creation"
       
   159     end
       
   160 
       
   161     def macaddr=(value)
       
   162         raise Puppet::Error, "macaddr property is settable only upon creation"
       
   163     end
       
   164 
       
   165     def uuid=(value)
       
   166         raise Puppet::Error, "uuid property is settable only upon creation"
       
   167     end
       
   168 
       
   169     # Add VPort Instance
       
   170     def create_vport(tenant, vport, properties)
       
   171         begin
       
   172             evsadm("add-vport", "-T", tenant, properties, vport)
       
   173         rescue Puppet::ExecutionFailure => e
       
   174             # Pass up the exception to upper level
       
   175             raise
       
   176         end
       
   177     end
       
   178 
       
   179     # Remove Vport Instance
       
   180     def delete_vport(tenant, vport)
       
   181         begin
       
   182             evsadm("remove-vport", "-T", tenant, vport)
       
   183         rescue Puppet::ExecutionFailure => e
       
   184             # Pass up the exception to upper level
       
   185             raise
       
   186         end
       
   187     end
       
   188     
       
   189     # set read/write property 
       
   190     def set_vportprop(tenant, vport, property)
       
   191         begin
       
   192             evsadm("set-vportprop", "-T", tenant, property, vport)
       
   193         rescue Puppet::ExecutionFailure => e
       
   194             # Pass up the exception to upper level
       
   195             raise
       
   196         end
       
   197     end
       
   198 
       
   199     # Parse the "name" value from user and yield tenant and vport name
       
   200     def get_tenant_and_vport_name
       
   201         fullname = @resource[:name]
       
   202 
       
   203         return [] if fullname == nil
       
   204         parsed_val = fullname.strip.split("/")
       
   205         if (parsed_val.length != 3)
       
   206             raise Puppet::Error, "Invalid VPort name #{@resource[:name]} \n" \
       
   207                 "Name convention must be <tenant>/<evs>/<vport>"
       
   208         end
       
   209         tenant, evs, vport = parsed_val
       
   210         return tenant, evs + "/" + vport
       
   211     end
       
   212 
       
   213     # property setter for vport creation
       
   214     def add_properties(source)
       
   215         p = []
       
   216         if source[:ipaddr] != nil
       
   217             source[:ipaddr] = source[:ipaddr].split('/')[0]
       
   218         end
       
   219         prop_list = {
       
   220             "cos" => source[:cos], 
       
   221             "maxbw" => source[:maxbw], 
       
   222             "priority" => source[:priority],
       
   223             "protection" => source[:protection],
       
   224             "ipaddr" => source[:ipaddr],
       
   225             "macaddr" => source[:macaddr],
       
   226             "uuid" => source[:uuid]
       
   227         }
       
   228         prop_list.each do |key, value|
       
   229             next if (value == "") || (value == nil)
       
   230             p << "#{key}=#{value}"
       
   231         end
       
   232         return [] if p.empty?
       
   233         properties = Array["-p", p.join(",")]
       
   234     end
       
   235 
       
   236     # Update property change
       
   237     def flush
       
   238         tenant, vport = get_tenant_and_vport_name
       
   239        
       
   240         # Update property values when specified
       
   241         unless @property_flush.empty?
       
   242             # update multiple property values iteratively
       
   243             @property_flush.each do |key, value|
       
   244                 prop = ["-p", "#{key}=#{value}"]
       
   245                 begin
       
   246                     set_vportprop(tenant, vport, prop)
       
   247                 rescue Puppet::ExecutionFailure => e
       
   248                     raise Puppet::Error, "Cannot update the property " \
       
   249                         "#{key}=#{value}.\n#{e.inspect}"
       
   250                 end
       
   251             end
       
   252         end
       
   253 
       
   254         # Synchronize all the SHOULD values to IS values
       
   255         @property_hash = resource.to_hash
       
   256     end
       
   257 end