Revision 0d9c9615
Added by Timo Goebel about 5 years ago
app/graphql/types/host.rb | ||
---|---|---|
belongs_to :operatingsystem, Types::Operatingsystem
|
||
belongs_to :puppet_ca_proxy, Types::SmartProxy
|
||
belongs_to :puppet_proxy, Types::SmartProxy
|
||
belongs_to :medium, Types::Medium
|
||
has_many :fact_names, Types::FactName
|
||
has_many :fact_values, Types::FactValue
|
||
end
|
app/graphql/types/medium.rb | ||
---|---|---|
module Types
|
||
class Medium < BaseObject
|
||
description 'A Medium'
|
||
|
||
global_id_field :id
|
||
timestamps
|
||
field :name, String
|
||
field :path, String
|
||
field :os_family, Types::OsFamilyEnum
|
||
|
||
has_many :operatingsystems, Types::Operatingsystem
|
||
has_many :hosts, Types::Host
|
||
has_many :locations, Types::Location
|
||
has_many :organizations, Types::Organization
|
||
end
|
||
end
|
app/graphql/types/operatingsystem.rb | ||
---|---|---|
field :title, String
|
||
field :type, String
|
||
field :fullname, String
|
||
field :family, Types::OsFamilyEnum
|
||
|
||
has_many :hosts, Types::Host
|
||
has_many :media, Types::Medium
|
||
end
|
||
end
|
app/graphql/types/query.rb | ||
---|---|---|
|
||
record_field :compute_attribute, Types::ComputeAttribute
|
||
collection_field :compute_attributes, Types::ComputeAttribute
|
||
|
||
record_field :medium, Types::Medium
|
||
collection_field :media, Types::Medium
|
||
end
|
||
end
|
test/factories/medium.rb | ||
---|---|---|
factory :medium do
|
||
sequence(:name) {|n| "medium#{n}" }
|
||
sequence(:path) {|n| "http://www.example.com/path#{n}" }
|
||
os_family { 'Redhat' }
|
||
organizations { [Organization.find_by_name('Organization 1')] }
|
||
locations { [Location.find_by_name('Location 1')] }
|
||
|
||
trait :coreos do
|
||
sequence(:name) { |n| "CoreOS Mirror #{n}"}
|
||
sequence(:path) {'http://$release.release.core-os.net'}
|
||
os_family { 'Coreos' }
|
||
end
|
||
|
||
trait :ubuntu do
|
||
sequence(:name) { |n| "Ubuntu Mirror #{n}"}
|
||
sequence(:path) {'http://archive.ubuntu.com/ubuntu'}
|
||
os_family { 'Debian' }
|
||
end
|
||
|
||
trait :debian do
|
||
sequence(:name) { |n| "Debian Mirror #{n}"}
|
||
sequence(:path) {'http://ftp.debian.org/debian'}
|
||
os_family { 'Debian' }
|
||
end
|
||
|
||
trait :suse do
|
||
sequence(:name) { |n| "OpenSuse Mirror #{n}"}
|
||
sequence(:path) {'http://mirror.isoc.org.il/pub/opensuse/distribution/$major.$minor/repo/oss'}
|
||
os_family { 'Suse' }
|
||
end
|
||
|
||
trait :with_operatingsystem do
|
||
operatingsystems { [FactoryBot.create(:operatingsystem, :with_archs)] }
|
||
end
|
||
end
|
||
end
|
test/graphql/queries/host_query_test.rb | ||
---|---|---|
puppetProxy {
|
||
id
|
||
}
|
||
medium {
|
||
id
|
||
}
|
||
factNames {
|
||
totalCount
|
||
edges {
|
||
... | ... | |
assert_record host.operatingsystem, data['operatingsystem']
|
||
assert_record host.puppet_ca_proxy, data['puppetCaProxy']
|
||
assert_record host.puppet_proxy, data['puppetProxy']
|
||
assert_record host.medium, data['medium']
|
||
|
||
assert_collection host.fact_names, data['factNames']
|
||
assert_collection host.fact_values, data['factValues']
|
test/graphql/queries/media_query_test.rb | ||
---|---|---|
require 'test_helper'
|
||
|
||
class Queries::MediaQueryTest < GraphQLQueryTestCase
|
||
let(:query) do
|
||
<<-GRAPHQL
|
||
query {
|
||
media {
|
||
totalCount
|
||
pageInfo {
|
||
startCursor
|
||
endCursor
|
||
hasNextPage
|
||
hasPreviousPage
|
||
}
|
||
edges {
|
||
cursor
|
||
node {
|
||
id
|
||
}
|
||
}
|
||
}
|
||
}
|
||
GRAPHQL
|
||
end
|
||
|
||
let(:data) { result['data']['media'] }
|
||
|
||
setup do
|
||
FactoryBot.create_list(:medium, 2)
|
||
end
|
||
|
||
test 'fetching media attributes' do
|
||
assert_empty result['errors']
|
||
|
||
expected_count = Medium.count
|
||
|
||
assert_not_equal 0, expected_count
|
||
assert_equal expected_count, data['totalCount']
|
||
assert_equal expected_count, data['edges'].count
|
||
end
|
||
end
|
test/graphql/queries/medium_query_test.rb | ||
---|---|---|
require 'test_helper'
|
||
|
||
class Queries::MediumQueryTest < GraphQLQueryTestCase
|
||
let(:query) do
|
||
<<-GRAPHQL
|
||
query (
|
||
$id: String!
|
||
) {
|
||
medium(id: $id) {
|
||
id
|
||
createdAt
|
||
updatedAt
|
||
name
|
||
path
|
||
osFamily
|
||
operatingsystems {
|
||
totalCount
|
||
edges {
|
||
node {
|
||
id
|
||
}
|
||
}
|
||
}
|
||
hosts {
|
||
totalCount
|
||
edges {
|
||
node {
|
||
id
|
||
}
|
||
}
|
||
}
|
||
locations {
|
||
totalCount
|
||
edges {
|
||
node {
|
||
id
|
||
}
|
||
}
|
||
}
|
||
organizations {
|
||
totalCount
|
||
edges {
|
||
node {
|
||
id
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
GRAPHQL
|
||
end
|
||
|
||
let(:medium) { FactoryBot.create(:medium, :with_operatingsystem) }
|
||
|
||
let(:global_id) { Foreman::GlobalId.for(medium) }
|
||
let(:variables) {{ id: global_id }}
|
||
let(:data) { result['data']['medium'] }
|
||
|
||
setup do
|
||
FactoryBot.create(:host, :managed, medium: medium, operatingsystem: medium.operatingsystems.first, architecture: medium.operatingsystems.first.architectures.first)
|
||
end
|
||
|
||
test 'fetching medium attributes' do
|
||
assert_empty result['errors']
|
||
|
||
assert_equal global_id, data['id']
|
||
assert_equal medium.created_at.utc.iso8601, data['createdAt']
|
||
assert_equal medium.updated_at.utc.iso8601, data['updatedAt']
|
||
assert_equal medium.name, data['name']
|
||
assert_equal medium.path, data['path']
|
||
assert_equal medium.os_family, data['osFamily']
|
||
|
||
assert_collection medium.operatingsystems, data['operatingsystems']
|
||
assert_collection medium.hosts, data['hosts'], type_name: 'Host'
|
||
assert_collection medium.locations, data['locations']
|
||
assert_collection medium.organizations, data['organizations']
|
||
end
|
||
end
|
test/graphql/queries/operatingsystem_query_test.rb | ||
---|---|---|
title
|
||
type
|
||
fullname
|
||
family
|
||
hosts {
|
||
totalCount
|
||
edges {
|
||
... | ... | |
}
|
||
}
|
||
}
|
||
media {
|
||
totalCount
|
||
edges {
|
||
node {
|
||
id
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
GRAPHQL
|
||
end
|
||
|
||
let(:hosts) { FactoryBot.create_list(:host, 2) }
|
||
let(:operatingsystem) { FactoryBot.create(:operatingsystem, hosts: hosts) }
|
||
let(:medium) { FactoryBot.create(:medium) }
|
||
let(:operatingsystem) { FactoryBot.create(:operatingsystem, family: 'Redhat', hosts: hosts, media: [medium]) }
|
||
|
||
let(:global_id) { Foreman::GlobalId.for(operatingsystem) }
|
||
let(:variables) {{ id: global_id }}
|
||
... | ... | |
assert_equal operatingsystem.title, data['title']
|
||
assert_equal operatingsystem.type, data['type']
|
||
assert_equal operatingsystem.fullname, data['fullname']
|
||
assert_equal operatingsystem.family, data['family']
|
||
|
||
assert_collection operatingsystem.hosts, data['hosts'], type_name: 'Host'
|
||
assert_collection operatingsystem.media, data['media']
|
||
end
|
||
end
|
Also available in: Unified diff
fixes #26492 - graphql: add media queries