fog-google icon indicating copy to clipboard operation
fog-google copied to clipboard

Add a `project` method to Images

Open Temikus opened this issue 6 years ago • 6 comments

See:

 # This attribute is not available in the representation of an
        # 'image' returned by the GCE server (see GCE API). However,
        # images are a global resource and a user can query for images
        # across projects. Therefore we try to remember which project
        # the image belongs to by tracking it in this attribute.
        attribute :project
def get(identity, project = nil)
          if project
            image = service.get_image(identity, project).to_h
            image[:project] = project
            return new(image)
          elsif identity
            project.nil? ? projects = all_projects : projects = [project]
            projects.each do |proj|
              begin
                response = service.get_image(identity, proj).to_h
                # TODO: Remove this - see #405
                response[:project] = proj
                image = response
                return new(image)
              rescue ::Google::Apis::ClientError => e
                next if e.status_code == 404
                break
              else
                break
              end
            end
            # If nothing is found - return nil
            nil
          end
        end

This is really not optimal. This information is already available in self_link and we should just add an object method instead of mixing it in during get request.

Temikus avatar Jul 22 '18 04:07 Temikus

Could you expand how this information is already available? The main issue iirc is that we had images across lots of projects that people were referencing by name.

icco avatar Jul 22 '18 23:07 icco

@icco If you look closely at the flow it's kinda weird.

get() method logic:

  1. Get the list of all available projects and iterate.
  2. Take next project.
  3. Check if get_image() returns something. If it does: a) Get the image parameter hash. b) Add the :project key to it and populate it with the project we found the image in. c) Create new image object from parameters. If it doesn't - rescue the 404 and go back to 2.

All the while the image self_link already contains the image reference with the project in it, e.g.:

https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-7-v20180716

, already contains the centos-cloud project.

In this case IMO it would be more logical to get the parameter out of something the API always provides rather than rely on brittle logic in get() to always set it.

I may be missing something though. So if I am - just let me know :)

Repro:

λ bundle exec rake console
[1] pry(main)> connection = Fog::Compute.new(:provider => "Google")
[2] pry(main)> connection.images.get('centos-7-v20180716')
=>   <Fog::Compute::Google::Image
    name="centos-7-v20180716",
    archive_size_bytes=13890626560,
    creation_timestamp="2018-07-17T10:04:24.974-07:00",
    deprecated=nil,
    description="CentOS, CentOS, 7, x86_64 built on 20180716",
    disk_size_gb=10,
    family="centos-7",
    guest_os_features=nil,
    id=4998083998998830967,
    image_encryption_key=nil,
    kind="compute#image",
    licenses=["https://www.googleapis.com/compute/v1/projects/centos-cloud/global/licenses/centos-7"],
    raw_disk={:container_type=>"TAR", :source=>""},
    self_link="https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-7-v20180716",
    source_disk=nil,
    source_disk_encryption_key=nil,
    source_disk_id=nil,
    source_image=nil,
    source_image_encryption_key=nil,
    source_image_id=nil,
    source_type="RAW",
    status="READY",
    project="centos-cloud"
  >

Temikus avatar Jul 23 '18 12:07 Temikus

So are you proposing just removing step 3?

icco avatar Jul 23 '18 22:07 icco

@icco I propose:

  1. Removing 3.b
  2. Adding a project method that will parse the self-link and return the image project, making the change effectively a no-op for users.

Temikus avatar Jul 24 '18 01:07 Temikus

Sorry for dragging this out a bit I think I need to get better at writing bugs 😭

Temikus avatar Jul 24 '18 01:07 Temikus

This issue has been marked inactive and will be closed if no further activity occurs.

github-actions[bot] avatar Apr 18 '21 02:04 github-actions[bot]