Managing schemas and APIs using the Service Registry Maven plug-in

Guide
  • Red Hat OpenShift Service Registry 1
  • Updated 02 December 2021
  • Published 24 September 2021

Managing schemas and APIs using the Service Registry Maven plug-in

Guide
Red Hat OpenShift Service Registry 1
  • Updated 02 December 2021
  • Published 24 September 2021

Manage schema and API artifacts stored in the Red Hat OpenShift Service Registry cloud service using the Service Registry Maven plug-in. For example, this includes adding, downloading, or testing schema and API artifacts during a Maven build.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code and documentation. We are beginning with these four terms: master, slave, blacklist, and whitelist. Due to the enormity of this endeavor, these changes will be gradually implemented over upcoming releases. For more details on making our language more inclusive, see our CTO Chris Wright’s message.

Adding schema and API artifacts using the Maven plug-in

The most common use case for the Maven plug-in is adding artifacts during a build. You can accomplish this by using the register execution goal.

Prerequisites
  • You have a service account with the correct access permissions for Service Registry instances

Procedure
  • Update your Maven pom.xml file to use the apicurio-registry-maven-plugin to register an artifact. The following example shows registering Apache Avro and GraphQL schemas:

    <plugin>
      <groupId>io.apicurio</groupId>
      <artifactId>apicurio-registry-maven-plugin</artifactId>
      <version>${apicurio.version}</version>
      <executions>
          <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>register</goal>  1
            </goals>
            <configuration>
                <registryUrl>http://REGISTRY-URL/apis/registry/v2</registryUrl> 2
                <authServerUrl>MY-AUTH-SERVER</authServerUrl>
                <clientId>MY-CLIENT-ID</clientId>
                <clientSecret>MY-CLIENT-SECRET</clientSecret> 3
                <artifacts>
                    <artifact>
                        <groupId>TestGroup</groupId> 4
                        <artifactId>FullNameRecord</artifactId>
                        <file>${project.basedir}/src/main/resources/schemas/record.avsc</file>
                        <ifExists>FAIL</ifExists>
                    </artifact>
                    <artifact>
                        <groupId>TestGroup</groupId>
                        <artifactId>ExampleAPI</artifactId> 5
                        <type>GRAPHQL</type>
                        <file>${project.basedir}/src/main/resources/apis/example.graphql</file>
                        <ifExists>RETURN_OR_UPDATE</ifExists>
                        <canonicalize>true</canonicalize>
                    </artifact>
                </artifacts>
            </configuration>
        </execution>
      </executions>
     </plugin>
    1. Specify register as the execution goal to upload the schema artifact to the registry.

    2. Specify the Service Registry URL with the ../apis/registry/v2 endpoint.

    3. Specify your service account ID and secret and the OpenShift Application Services authentication server: https://identity.api.openshift.com/auth/realms/rhoas/protocol/openid-connect/token

    4. Specify the Service Registry artifact group ID. You can specify the default group if you do not want to use a unique group.

    5. You can upload multiple artifacts using the specified group ID, artifact ID, and location.

Downloading schema and API artifacts using the Maven plug-in

You can use the Maven plug-in to download artifacts from Service Registry. This is often useful, for example, when generating code from a registered schema.

Prerequisites
  • You have service account with the correct access permissions for Service Registry instances

Procedure
  • Update your Maven pom.xml file to use the apicurio-registry-maven-plugin to download an artifact. The following example shows downloading Apache Avro and GraphQL schemas.

    <plugin>
      <groupId>io.apicurio</groupId>
      <artifactId>apicurio-registry-maven-plugin</artifactId>
      <version>${apicurio.version}</version>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>download</goal> 1
          </goals>
          <configuration>
              <registryUrl>http://REGISTRY-URL/apis/registry/v2</registryUrl> 2
              <authServerUrl>MY-AUTH-SERVER</authServerUrl>
              <clientId>MY-CLIENT-ID</clientId>
              <clientSecret>MY-CLIENT-SECRET</clientSecret> 3
              <artifacts>
                  <artifact>
                      <groupId>TestGroup</groupId> 4
                      <artifactId>FullNameRecord</artifactId> 5
                      <file>${project.build.directory}/classes/record.avsc</file>
                      <overwrite>true</overwrite>
                  </artifact>
                  <artifact>
                      <groupId>TestGroup</groupId>
                      <artifactId>ExampleAPI</artifactId>
                      <version>1</version>
                      <file>${project.build.directory}/classes/example.graphql</file>
                      <overwrite>true</overwrite>
                  </artifact>
              </artifacts>
          </configuration>
        </execution>
      </executions>
    </plugin>
    1. Specify download as the execution goal.

    2. Specify the Service Registry URL with the ../apis/registry/v2 endpoint.

    3. Specify your service account ID and secret and the OpenShift Application Services authentication server: https://identity.api.openshift.com/auth/realms/rhoas/protocol/openid-connect/token

    4. Specify the Service Registry artifact group ID. You can specify the default group if you do not want to use a unique group.

    5. You can download multiple artifacts to a specified directory using the artifact ID.

Testing schema and API artifacts using the Maven plug-in

You might want to verify that an artifact can be registered without actually making any changes. This is often useful when rules are configured in Service Registry. Testing the artifact results in a failure if the artifact content violates any of the configured rules.

When testing artifacts using the Maven plug-in, even if the artifact passes the test, no content is added to Service Registry.
Prerequisites
  • You must have a service account with the correct access permissions for Service Registry instances

Procedure
  • Update your Maven pom.xml file to use the apicurio-registry-maven-plugin to test an artifact. The following example shows testing an Apache Avro schema:

    <plugin>
      <groupId>io.apicurio</groupId>
      <artifactId>apicurio-registry-maven-plugin</artifactId>
      <version>${apicurio.version}</version>
      <executions>
          <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>test-update</goal>  1
            </goals>
            <configuration>
                <registryUrl>http://REGISTRY-URL/apis/registry/v2</registryUrl> 2
                <authServerUrl>MY-AUTH-SERVER</authServerUrl>
                <clientId>MY-CLIENT-ID</clientId>
                <clientSecret>MY-CLIENT-SECRET</clientSecret> 3
                <artifacts>
                    <artifact>
                        <groupId>TestGroup</groupId> 4
                        <artifactId>FullNameRecord</artifactId>
                        <file>${project.basedir}/src/main/resources/schemas/record.avsc</file> 5
                    </artifact>
                    <artifact>
                        <groupId>TestGroup</groupId>
                        <artifactId>ExampleAPI</artifactId>
                        <type>GRAPHQL</type>
                        <file>${project.basedir}/src/main/resources/apis/example.graphql</file>
                    </artifact>
                </artifacts>
            </configuration>
        </execution>
      </executions>
     </plugin>
    1. Specify test-update as the execution goal to test the schema artifact.

    2. Specify the Service Registry URL with the ../apis/registry/v2 endpoint.

    3. Specify your service account ID and secret and the OpenShift Application Services authentication server: https://identity.api.openshift.com/auth/realms/rhoas/protocol/openid-connect/token

    4. Specify the Service Registry artifact group ID. You can specify the default group if you do not want to use a unique group.

    5. You can test multiple artifacts from a specified directory using the artifact ID.