|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright 2025 Perforce Software Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +require_relative "base_with_port" |
| 18 | + |
| 19 | +module PEClient |
| 20 | + module Resource |
| 21 | + # Server-specific Puppet API endpoints. |
| 22 | + # |
| 23 | + # @see https://help.puppet.com/core/current/Content/PuppetCore/server/http_api/puppet_v3_api.htm |
| 24 | + class PuppetV3 < BaseWithPort |
| 25 | + # The base path for Puppet API v3 endpoints. |
| 26 | + BASE_PATH = "/puppet/v3" |
| 27 | + |
| 28 | + # Default Puppet API Port |
| 29 | + PORT = 8140 |
| 30 | + |
| 31 | + # Returns a catalog for the specified node name given the provided facts. |
| 32 | + # |
| 33 | + # @param node_name [String] The name of the node to retrieve the catalog for. |
| 34 | + # @param environment [String] The environment to use when compiling the catalog. |
| 35 | + # |
| 36 | + # @return [Hash] |
| 37 | + # |
| 38 | + # @see https://help.puppet.com/core/current/Content/PuppetCore/server/http_api/http_catalog.htm |
| 39 | + def catalog(node_name:, environment: nil) |
| 40 | + @client.get File.join("#{BASE_PATH}/catalog", node_name), params: {environment:}.compact |
| 41 | + end |
| 42 | + |
| 43 | + # The returned information includes the node name and environment, and optionally any classes set by an External Node Classifier and a hash of parameters which may include the node's facts. |
| 44 | + # The returned node may have a different environment from the one given in the request if Puppet is configured with an ENC. |
| 45 | + # |
| 46 | + # @param certname [String] The name of the node to retrieve information for. |
| 47 | + # @param environment [String] The environment to use when retrieving the node information. |
| 48 | + # @param transaction_uuid [String] A transaction uuid identifying the entire transaction (shows up in the report as well). |
| 49 | + # @param configured_environment [String] The environment configured on the client. |
| 50 | + # |
| 51 | + # @return [Hash] |
| 52 | + # |
| 53 | + # @see https://help.puppet.com/core/current/Content/PuppetCore/server/http_api/http_node.htm |
| 54 | + def node(certname:, environment: nil, transaction_uuid: nil, configured_environment: nil) |
| 55 | + @client.get File.join("#{BASE_PATH}/node", certname), params: {environment:, transaction_uuid:, configured_environment:}.compact |
| 56 | + end |
| 57 | + |
| 58 | + # Allows setting the facts for the specified node name. |
| 59 | + # |
| 60 | + # @param node_name [String] The name of the node to set the facts for. |
| 61 | + # @param facts [Hash] The facts to set for the node. |
| 62 | + # @param environment [String] The environment to use when setting the facts. |
| 63 | + # |
| 64 | + # @return [Hash] |
| 65 | + # |
| 66 | + # @see https://help.puppet.com/core/current/Content/PuppetCore/server/http_api/http_facts.htm |
| 67 | + def facts(node_name:, facts:, environment: nil) |
| 68 | + @client.put File.join("#{BASE_PATH}/facts", node_name), body: facts.to_json, params: {environment:}.compact |
| 69 | + end |
| 70 | + |
| 71 | + # Returns the contents of the specified file. |
| 72 | + # |
| 73 | + # @param mount_point [String] One of the following types: |
| 74 | + # - Custom file serving mounts as specified in fileserver.conf |
| 75 | + # - `modules/<MODULE>` --- a semi-magical mount point which allows access to the files subdirectory of <MODULE> |
| 76 | + # - `plugins` --- a highly magical mount point which merges the lib directory of every module together. |
| 77 | + # Used for syncing plugins; not intended for general consumption. |
| 78 | + # Per-module sub-paths can not be specified. |
| 79 | + # - `pluginfacts` --- a highly magical mount point which merges the facts.d directory of every module together. |
| 80 | + # Used for syncing external facts; not intended for general consumption. |
| 81 | + # Per-module sub-paths can not be specified. |
| 82 | + # - `tasks/<MODULE>` --- a semi-magical mount point which allows access to files in the tasks subdirectory of <MODULE> |
| 83 | + # @param name [String] |
| 84 | + # |
| 85 | + # @return [String] |
| 86 | + # |
| 87 | + # @see https://help.puppet.com/core/current/Content/PuppetCore/server/http_api/http_file_content.htm |
| 88 | + def file_content(mount_point:, name:) |
| 89 | + @client.get File.join("#{BASE_PATH}/file_content", mount_point, name), params: {"Content-Type": "application/octet-stream", Accept: "application/octet-stream"}.freeze |
| 90 | + end |
| 91 | + |
| 92 | + # This endpoint allows clients to send reports to the master. |
| 93 | + # Once received by the master they are processed by the `report processors` configured to be triggered when a report is received. |
| 94 | + # As an example, storing reports in PuppetDB is handled by one such report processor. |
| 95 | + # |
| 96 | + # @param node_name [String] The name of the node the report is for. |
| 97 | + # @param environment [String] The environment to use when submitting the report. |
| 98 | + # @param report [Hash] The report to submit. |
| 99 | + # |
| 100 | + # @return [Hash] |
| 101 | + # |
| 102 | + # @see https://help.puppet.com/core/current/Content/PuppetCore/server/http_api/http_report.htm |
| 103 | + def report(node_name:, environment:, report:) |
| 104 | + @client.put File.join("#{BASE_PATH}/report", node_name), body: report, params: {environment:} |
| 105 | + end |
| 106 | + |
| 107 | + # The environment classes API serves as a replacement for the Puppet resource type API for classes, which was removed in Puppet. |
| 108 | + # |
| 109 | + # @param environment [String] The environment to query. |
| 110 | + # |
| 111 | + # @return [Hash] |
| 112 | + # |
| 113 | + # @see https://help.puppet.com/core/current/Content/PuppetCore/server/http_api/puppet-api/v3/environment_classes.htm |
| 114 | + def environment_classes(environment:) |
| 115 | + @client.get "#{BASE_PATH}/environment_classes", params: {environment:} |
| 116 | + end |
| 117 | + |
| 118 | + # The environment modules API returns information about what modules are installed for the requested environment. |
| 119 | + # |
| 120 | + # @param environment [String] The environment to query. |
| 121 | + # |
| 122 | + # @return [Array<Hash>, Hash] |
| 123 | + # |
| 124 | + # @see https://help.puppet.com/core/current/Content/PuppetCore/server/http_api/puppet-api/v3/environment_modules.htm |
| 125 | + def environment_modules(environment: nil) |
| 126 | + @client.get "#{BASE_PATH}/environment_modules", params: {environment:}.compact |
| 127 | + end |
| 128 | + |
| 129 | + # The static_file_content endpoint returns the standard output of a `code-content-command` script, which should output the contents of a specific version of a `file resource` that has a source attribute with a `puppet:///` URI value. |
| 130 | + # That source must be a file from the files or tasks directory of a module in a specific environment. |
| 131 | + # |
| 132 | + # @param file_path [String] The path corresponds to the requested file's path on the Server relative to the given environment's root directory, and must point to a file in the */*/files/**, */*/lib/**, */*/scripts/**, or */*/tasks/** glob. |
| 133 | + # |
| 134 | + # @return [String] |
| 135 | + # |
| 136 | + # @see https://help.puppet.com/core/current/Content/PuppetCore/server/http_api/puppet-api/v3/static_file_content.htm |
| 137 | + def static_file_content(file_path:) |
| 138 | + @client.get File.join("#{BASE_PATH}/static_file_content", file_path) |
| 139 | + end |
| 140 | + |
| 141 | + # @return [PEClient::Resource::PuppetV3::FileBucket] |
| 142 | + def file_bucket |
| 143 | + require_relative "puppet.v3/file_bucket" |
| 144 | + @file_bucket ||= PuppetV3::FileBucket.new(@client) |
| 145 | + end |
| 146 | + |
| 147 | + # @return [PEClient::Resource::PuppetV3::FileMetadata] |
| 148 | + def file_metadata |
| 149 | + require_relative "puppet.v3/file_metadata" |
| 150 | + @file_metadata ||= PuppetV3::FileMetadata.new(@client) |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | +end |
0 commit comments