-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Add new resource: network_interfaces_eni #32
Open
jeffbyrnes
wants to merge
1
commit into
main
Choose a base branch
from
add-network_interfaces_eni
base: main
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 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 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 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,121 @@ | ||
require 'aws-sdk' | ||
require 'net/http' | ||
|
||
default_action :create_and_attach | ||
|
||
property :description, String | ||
property :subnet_id, String, required: true | ||
property :private_ip_address, String | ||
property :security_groups, Array | ||
property :network_interface_id, String | ||
|
||
action :create_and_attach do | ||
nic_id_to_attach = network_interface_id || create | ||
|
||
if attached_nic_ids.include? nic_id_to_attach | ||
Chef::Log.debug "NIC with ID #{nic_id_to_attach} is already attached to #{instance_id}" | ||
else | ||
converge_by "Attach NIC #{nic_id_to_attach} to #{instance_id} at index #{next_device_index}" do | ||
ec2.attach_network_interface( | ||
network_interface_id: nic_id_to_attach, | ||
instance_id: instance_id, | ||
device_index: next_device_index | ||
) | ||
end | ||
end | ||
end | ||
|
||
action :create do | ||
create | ||
end | ||
|
||
action :delete do | ||
if existing_nic | ||
converge_by "Delete the NIC with ID #{existing_nic.network_interface_id}" do | ||
ec2.delete_network_interface network_interface_id: existing_nic.network_interface_id | ||
end | ||
elsif network_interface_id | ||
Chef::Log.debug "NIC with ID #{network_interface_id} does not exist" | ||
else | ||
Chef::Log.debug "NIC with description \"#{description}\" does not exist" | ||
end | ||
end | ||
|
||
private | ||
|
||
def create | ||
if existing_nic_id | ||
Chef::Log.debug("NIC already exists: #{existing_nic.network_interface_id}/#{description}") | ||
return existing_nic.network_interface_id | ||
end | ||
|
||
converge_by "Create new NIC in description: #{description}, subnet_id: #{subnet_id}" do | ||
options = {} | ||
|
||
%w(subnet_id private_ip_address security_groups).each do |prop| | ||
next unless send prop | ||
options[prop.to_sym] = send(prop) | ||
end | ||
|
||
ec2.create_network_interface(options).network_interface.network_interface_id | ||
end | ||
end | ||
|
||
def existing_nic | ||
@existing_nic ||= begin | ||
# Use the ID to look up the adapter if we have it | ||
return ec2.describe_network_interfaces( | ||
network_interface_id: network_interface_id | ||
).network_interfaces.first if network_interface_id | ||
|
||
# Otherwise use the description | ||
results = ec2.describe_network_interfaces( | ||
filters: [{ name: 'description', values: [description] }] | ||
).network_interfaces | ||
|
||
# Multiple NICs with the same description == problems | ||
if results.count > 1 | ||
fail "More than one NIC matches the description \"#{description}\": " \ | ||
"#{results.map(&:network_interface_id).join ', '}" | ||
end | ||
|
||
results.first | ||
end | ||
end | ||
|
||
def ec2 | ||
@ec2 ||= AWS::EC2::Client.new | ||
end | ||
|
||
def instance_id | ||
@instance_id ||= Net::HTTP.get URI 'http://169.254.169.254/2016-09-02/meta-data/instance-id' | ||
end | ||
|
||
def next_device_index | ||
@next_device_index ||= begin | ||
device_numbers = macs.map do |mac| | ||
Net::HTTP.get( | ||
URI "http://169.254.169.254/2016-09-02/meta-data/network/interfaces/macs/#{mac}/device-number" | ||
).to_i | ||
end | ||
|
||
device_numbers.sort.last + 1 | ||
end | ||
end | ||
|
||
def macs | ||
@macs ||= Net::HTTP.get( | ||
URI 'http://169.254.169.254/2016-09-02/meta-data/network/interfaces/macs/' | ||
).delete('/').split("\n") | ||
end | ||
|
||
def attached_nic_ids | ||
@attached_nic_ids ||= begin | ||
macs.map do |mac| | ||
Net::HTTP.get( | ||
URI "http://169.254.169.254/2016-09-02/meta-data/network/interfaces/macs/#{mac}" \ | ||
'/interface-id' | ||
) | ||
end | ||
end | ||
end |
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.
All these can be added to the
action_class
block rather than adding them to the 'outer' resource class.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.
https://docs.chef.io/custom_resources/#action_class
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.
Thanks @bmhughes! I did a big refactor in #44, so once that merges, and I rebase #43 & get it merged, I’ll come back to this one.