fog / fog-google

Fog for Google Cloud Platform
MIT License
99 stars 146 forks source link

Compute metadata supported? #388

Closed jnearing-kabam closed 5 years ago

jnearing-kabam commented 5 years ago

Does fog-google support ComputeMetadata?

...As a more general question, does Fog-google have any documentation? I've been just stitching together functionality by skimming the models, but having some actual documentation would be nice. I didn't see anything on README pointing to it or in the codebase itself.

Temikus commented 5 years ago

First of all, this should do what you need:

1) When creating a server:

  connection = Fog::Compute.new(:provider => "Google")

  disk = connection.disks.create(
    :name => "fog-smoke-test-#{Time.now.to_i}",
    :size_gb => 10,
    :zone => "us-central1-f",
    :source_image => "debian-9-stretch-v20180611"
  )

  disk.wait_for { disk.ready? }

  binding.pry

  server = connection.servers.create(
    :name => "fog-smoke-test-#{Time.now.to_i}",
    :disks => [disk],
    :machine_type => "n1-standard-1",
    :private_key_path => File.expand_path("~/.ssh/id_rsa"),
    :public_key_path => File.expand_path("~/.ssh/id_rsa.pub"),
    :zone => "us-central1-f",
    :username => ENV["USER"],
    :metadata => {:items => [{ :key => "foo", :value => "bar" }]},
    :tags => ["fog"],
    :service_accounts => { :scopes => %w(sql-admin bigquery https://www.googleapis.com/auth/compute) }
  )

2) Setting metadata on a server object:

server.set_metadata({ "foo" => "bar", "baz" => "foo" })
Temikus commented 5 years ago

Concerning docs - they are in poor state, this is one of the priorities for me to fix up and we have started tracking doc coverage this month. If you can tell me what are the first questions that come to mind it would be highly appreciated as this will give me some ideas what to prioritise.

Resources in the meantime:

jnearing-kabam commented 5 years ago

The use case I have is for retrieving metadata for an instance already created independent of Fog.

Essentially I have a Chef script running on a VM to do some operations, using Fog to interact with whatever cloud its on. I dont know what the server metadata [name,machine_type,description,etc] is in advance. If I want an instance to say take a snapshot, I need a way of knowing what instance_id to operate on.

GCP gives you a metadata api to retrieve exactly this kind of data, but I don't see it exposed through fog-google.

I just wanted to ensure I wasn't missing the functionality in Fog, it's easy enough to curl for that data.


For the docs side of things, I think first thing is to get something on the README, even if its just 'hey we know the docs are hurting'. Examples are a good way to get started with foundational things like getting your Fog connection established, so keeping them up to date should probably be a priority.

The code is generally clean enough to parse for functionality (good job on that btw), it just takes some time to figure out where the models you need are. Maybe some documentation around code structure can help point people to where they need to go.

Temikus commented 5 years ago

@jnearing-kabam

Ah, I think I get it now, you mean this, correct?

curl -s http://metadata/computeMetadata/v1/instance/?recursive=true -H "Metadata-Flavor: Google" |  to_j
{
  "attributes": {
  },
  "cpuPlatform": "Intel Broadwell",
  "description": "",
  "disks": [
    {
      "deviceName": "persistent-disk-0",
      "index": 0,
      "mode": "READ_WRITE",
      "type": "PERSISTENT-SSD"
    }
  ],

If so - it's not in the compute API I'm afraid. The one you've linked is custom instance metadata, which we do support, however, the information above is not retrievable through it.

However, if I'm missing something - just let me know!


Thank you for good doc suggestions, I'll try to get to it this week while I have some cycles.

jnearing-kabam commented 5 years ago

@Temikus Yeah thats the data I'm after. It's easy enough to retrieve that data, I just wanted to do it through Fog if available.

I'm going to close this issue, thanks for the help.