HWRP Array attribute not reset

I’m working with a custom HWRP, and I have an attribute of type Array.

The definition is below, and the issue I’m trying to trouble shoot is that if I update the value of my array, instead of resetting the array, they new values are appended to the array.

def external_addresses(arg = nil)
  set_or_return(:external_addresses, arg, :kind_of => Array)
end

Thanks for any feedback,
Curtis

Can you give an example of how you are using it?

--Noah

On Mar 11, 2014, at 3:19 PM, "Stewart, Curtis" cstewart@momentumsi.com wrote:

I’m working with a custom HWRP, and I have an attribute of type Array.

The definition is below, and the issue I’m trying to trouble shoot is that if I update the value of my array, instead of resetting the array, they new values are appended to the array.

def external_addresses(arg = nil)
  set_or_return(:external_addresses, arg, :kind_of => Array)
end

Thanks for any feedback,
Curtis

Attribute Sample:

node[‘servers’][‘main’][‘external_addresses’] = %w { 33.33.33.33 }

Recipe Context:

node[’servers’].each do |name, options|
server name do
external_addresses options[‘external_addresses’]
end
end

Then, if I update that attribute value to, say %w{ 33.33.33.30 }, the resource loads with both addresses.

Curtis

On Mar 11, 2014, at 5:20 PM, Noah Kantrowitz <noah@coderanger.netmailto:noah@coderanger.net> wrote:

Can you give an example of how you are using it?

–Noah

On Mar 11, 2014, at 3:19 PM, “Stewart, Curtis” <cstewart@momentumsi.commailto:cstewart@momentumsi.com> wrote:

I’m working with a custom HWRP, and I have an attribute of type Array.

The definition is below, and the issue I’m trying to trouble shoot is that if I update the value of my array, instead of resetting the array, they new values are appended to the array.

def external_addresses(arg = nil)
set_or_return(:external_addresses, arg, :kind_of => Array)
end

Thanks for any feedback,
Curtis

You are probably running in to the merge behavior of arrays in node attributes. Check out Arrays and Chef Attributes – Noah Kantrowitz for more info.

--Noah

On Mar 11, 2014, at 3:35 PM, "Stewart, Curtis" cstewart@momentumsi.com wrote:

Attribute Sample:

node[‘servers’][‘main’][‘external_addresses’] = %w { 33.33.33.33 }

Recipe Context:

node[’servers'].each do |name, options|
server name do
external_addresses options['external_addresses']
end
end

Then, if I update that attribute value to, say %w{ 33.33.33.30 }, the resource loads with both addresses.

Curtis

On Mar 11, 2014, at 5:20 PM, Noah Kantrowitz noah@coderanger.net wrote:

Can you give an example of how you are using it?

--Noah

On Mar 11, 2014, at 3:19 PM, "Stewart, Curtis" cstewart@momentumsi.com wrote:

I’m working with a custom HWRP, and I have an attribute of type Array.

The definition is below, and the issue I’m trying to trouble shoot is that if I update the value of my array, instead of resetting the array, they new values are appended to the array.

def external_addresses(arg = nil)
set_or_return(:external_addresses, arg, :kind_of => Array)
end

Thanks for any feedback,
Curtis

Thanks for the article!

I attempted to add a helper method to convert the options before passing, still no luck. I’d really like to stay away from setting these values to hashes.

def mutable_arrray(attribute)
attribute.map { |value| value }
end

I’m surprised this behavior doesn’t come up more often when using built in resources (i.e. path attribute for execute resource).

Curtis

On Mar 11, 2014, at 5:39 PM, Noah Kantrowitz <noah@coderanger.netmailto:noah@coderanger.net> wrote:

You are probably running in to the merge behavior of arrays in node attributes. Check out https://coderanger.net/2013/06/arrays-and-chef/ for more info.

–Noah

On Mar 11, 2014, at 3:35 PM, “Stewart, Curtis” <cstewart@momentumsi.commailto:cstewart@momentumsi.com> wrote:

Attribute Sample:

node[‘servers’][‘main’][‘external_addresses’] = %w { 33.33.33.33 }

Recipe Context:

node[’servers’].each do |name, options|
server name do
external_addresses options[‘external_addresses’]
end
end

Then, if I update that attribute value to, say %w{ 33.33.33.30 }, the resource loads with both addresses.

Curtis

On Mar 11, 2014, at 5:20 PM, Noah Kantrowitz <noah@coderanger.netmailto:noah@coderanger.net> wrote:

Can you give an example of how you are using it?

–Noah

On Mar 11, 2014, at 3:19 PM, “Stewart, Curtis” <cstewart@momentumsi.commailto:cstewart@momentumsi.com> wrote:

I’m working with a custom HWRP, and I have an attribute of type Array.

The definition is below, and the issue I’m trying to trouble shoot is that if I update the value of my array, instead of resetting the array, they new values are appended to the array.

def external_addresses(arg = nil)
set_or_return(:external_addresses, arg, :kind_of => Array)
end

Thanks for any feedback,
Curtis

If you want sane deep merge semantics, you have to not use arrays. Otherwise just ensure only ever set things once and you should be fine.

--Noah

On Mar 11, 2014, at 4:14 PM, "Stewart, Curtis" cstewart@momentumsi.com wrote:

Thanks for the article!

I attempted to add a helper method to convert the options before passing, still no luck. I’d really like to stay away from setting these values to hashes.

def mutable_arrray(attribute)
attribute.map { |value| value }
end

I’m surprised this behavior doesn’t come up more often when using built in resources (i.e. path attribute for execute resource).

Curtis

On Mar 11, 2014, at 5:39 PM, Noah Kantrowitz noah@coderanger.net wrote:

You are probably running in to the merge behavior of arrays in node attributes. Check out Arrays and Chef Attributes – Noah Kantrowitz for more info.

--Noah

On Mar 11, 2014, at 3:35 PM, "Stewart, Curtis" cstewart@momentumsi.com wrote:

Attribute Sample:

node[‘servers’][‘main’][‘external_addresses’] = %w { 33.33.33.33 }

Recipe Context:

node[’servers'].each do |name, options|
server name do
external_addresses options['external_addresses']
end
end

Then, if I update that attribute value to, say %w{ 33.33.33.30 }, the resource loads with both addresses.

Curtis

On Mar 11, 2014, at 5:20 PM, Noah Kantrowitz noah@coderanger.net wrote:

Can you give an example of how you are using it?

--Noah

On Mar 11, 2014, at 3:19 PM, "Stewart, Curtis" cstewart@momentumsi.com wrote:

I’m working with a custom HWRP, and I have an attribute of type Array.

The definition is below, and the issue I’m trying to trouble shoot is that if I update the value of my array, instead of resetting the array, they new values are appended to the array.

def external_addresses(arg = nil)
set_or_return(:external_addresses, arg, :kind_of => Array)
end

Thanks for any feedback,
Curtis