Utilizzo dei set di proprietà

Questa pagina si applica a Apigee e Apigee ibridi.

Visualizza documentazione di Apigee Edge.

Un insieme di proprietà è una raccolta personalizzata di coppie chiave/valore in cui vengono archiviati i dati. I proxy API possono recuperare questi dati quando vengono eseguiti.

Perché utilizzare gli insiemi di proprietà?

In genere, si utilizzano gli insiemi di proprietà per archiviare i dati non in scadenza che non devono essere hardcoded nella logica del proxy API. Puoi accedere ai dati dei set di proprietà in qualsiasi punto di un proxy in cui possono accedere alle variabili di flusso.

Un caso d'uso comune per gli insiemi di proprietà è fornire valori che associati a un ambiente o all'altro. Ad esempio, puoi crea una proprietà basata sull'ambiente e impostata con valori di configurazione specifiche per i proxy in esecuzione nell'ambiente di test e un altro per il tuo ambiente di produzione.

Ad esempio:

  • L'insieme di proprietà prod-env.properties contiene la proprietà log-level=error
  • L'insieme di proprietà test-env.properties contiene la proprietà log-level=debug

Allo stesso modo, potresti archiviare informazioni di routing specifiche dell'ambiente:

  • L'insieme di proprietà test-env.properties contiene la proprietà db-url=mydomain.test.datasource.com
  • L'insieme di proprietà prod-env.properties contiene la proprietà db-url=mydomain.prod.datasource.com

Ambiti del set di proprietà

Puoi definire l'ambito di una proprietà impostata su una revisione o un ambiente del proxy API quando crei la insieme di proprietà. Non puoi definire l'ambito di una proprietà impostata su un'organizzazione.

La seguente tabella spiega il comportamento e l'amministrazione degli insiemi di proprietà con il proxy API e l'ambito dell'organizzazione:

Ambito Comportamento di runtime Amministrazione
proxy API Le proprietà sono disponibili solo per la revisione del proxy API che contiene la proprietà insieme di proprietà. Nessun altro proxy API o revisione dello stesso proxy può accedere all'elemento un particolare insieme di proprietà. Gli amministratori possono utilizzare lo /resourcefiles o l'API Apigee o la UI per creare e modificare i set di proprietà. Il salvataggio del proxy API nella UI creerà una nuova revisione e l'insieme di proprietà modificato verrà associato solo a quella revisione.
Ambiente Le proprietà sono disponibili per tutte le revisioni di tutti i proxy API all'interno di quell'ambiente. I proxy API all'interno di altri ambienti non possono accedere a questo insieme di proprietà. Gli amministratori devono utilizzare /resourcefiles API Apigee per creare, visualizzare Aggiornare o eliminare gli insiemi di proprietà basati sull'ambiente. Questi insiemi di proprietà non sono visualizzato e non può essere modificato nella UI di Apigee.

Limiti impostati per le proprietà

Apigee impone dei limiti agli insiemi di proprietà, come descritto in Limiti. Inoltre, i file degli insiemi di proprietà devono utilizzare lo stesso come proprietà Java , con una sola eccezione: non puoi utilizzare parentesi graffe {} nei file degli insiemi di proprietà.

Creare file di insiemi di proprietà

In genere, i valori degli insiemi di proprietà vengono memorizzati come coppie nome/valore in un file. I file del set di proprietà vengono file di risorse di tipo properties.

I file di set di proprietà supportano la stessa sintassi dei Proprietà Java file; ad esempio, possono contenere valori Unicode e usare # o ! come indicatori di commento. Nota questa unica eccezione: non puoi utilizzare parentesi graffe {} nei file degli insiemi di proprietà.

Devi aggiungere il suffisso .properties al nome del file di una proprietà. Ad esempio: myconfig.my_key.properties è consentito, ma myconfig.my_key non è consentito.

La struttura della specifica di un insieme di proprietà è: property_set_name.property_name.properties Il nome dell'insieme di proprietà e il nome della proprietà non possono contenere punti. Ad esempio: myconfig.my_key.properties è consentito, ma myconfig.my.key.properties e Non sono consentiti my.config.my_key.properties.

L'esempio seguente mostra un semplice file di set di proprietà che definisce diverse proprietà:

# myProps.properties file
# General properties
foo=bar
baz=biff

# Messages/notes/warnings
message=This is a basic message.
note_message=This is an important message.
error_message=This is an error message.

# Keys
publickey=abc123
privatekey=splitwithsoundman

Dopo aver creato un file di set di proprietà, devi caricarlo su Apigee utilizzando UI o API.

Gestire gli insiemi di proprietà tramite l'interfaccia utente

Gestisci i set di proprietà con ambito a una revisione del proxy API utilizzando l'interfaccia utente nello stesso modo in cui gestisci altre risorse. Per saperne di più, vedi Gestire le risorse utilizzando l'interfaccia utente.

Gestire i set di proprietà utilizzando l'API

Gestisci i set di proprietà con ambito a una revisione o a un ambiente del proxy API utilizzando l'API, come descritto nelle sezioni seguenti.

Creare set di proprietà utilizzando l'API

Le seguenti sezioni descrivono come creare set di proprietà con ambito a una revisione del proxy API o a un ambiente utilizzando l'API.

Creare set di proprietà con ambito a una revisione del proxy API utilizzando l'API

Per creare un set di proprietà che abbia come ambito una revisione del proxy API utilizzando l'API:

  1. Crea il file dell'insieme di proprietà.
  2. Aggiungi il file del set di proprietà a un pacchetto di configurazione del proxy API.
  3. Carica il bundle tramite Crea proxy API oppure API di aggiornamento della revisione del proxy API.

Creare set di proprietà con ambito a un ambiente utilizzando l'API

Per aggiungere un insieme di proprietà che abbia come ambito un ambiente utilizzando l'API, crea il file del set di proprietà e poi caricalo in un ambiente della organizzazione inviando una richiesta POST alla seguente risorsa: https://apigee.googleapis.com/v1/organizations/{org}/environments/{env}/resourcefiles

Includi le seguenti informazioni nella richiesta:

  • Imposta il parametro di query name sul nome dell'insieme di proprietà
  • Imposta il parametro di query type su properties
  • Trasmetti i contenuti del file del set di proprietà come application/octet-stream o multipart/form-data

L'esempio seguente importa un set di proprietà denominato MyPropSet dal file /Users/myhome/myprops.properties:

curl -X POST "https://apigee.googleapis.com/v1/organizations/my-organization/environments/test/resourcefiles?name=MyPropSet&type=properties"
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-type: multipart/form-data" \
  -F file=@/Users/myhome/myprops.properties
  

$TOKEN is set to your OAuth 2.0 access token, as described in Obtaining an OAuth 2.0 access token. For information about the curl options used in this example, see Using curl.

The following provides an example of the response.

{
  "name": "MyPropSet",
  "type": "properties"
}

For more information, see Create resource file API.

View property sets using the API

The following sections describe how to view property sets scoped to an environment using the API.

View all property sets scoped to an environment using the API

To view all property sets scoped to an environment using the API, issue a GET request to the following resource: https://apigee.googleapis.com/v1/organizations/{org}/environments/{env}/resourcefiles/properties

The following example lists all property sets in the test environment:

curl -X GET "https://apigee.googleapis.com/v1/organizations/my-organization/environments/test/resourcefiles/properties" \
  -H "Authorization: Bearer $TOKEN"
  

$TOKEN is set to your OAuth 2.0 access token, as described in Obtaining an OAuth 2.0 access token. For information about the curl options used in this example, see Using curl.

The following provides an example of the response.

{
  "resourceFile": [
    {
      "name": "MyPropSet",
      "type": "properties"
    }
  ]
}

For more information, see List environment resource files API.

View the contents of a property set scoped to an environment using the API

To view the contents of a property set scoped to an environment using the API, issue a GET request to the following resource: https://apigee.googleapis.com/v1/organizations/{org}/environments/{env}/resourcefiles/properties/name

The following example displays the contents of the MyPropSet property set in the test environment:

curl -X GET "https://apigee.googleapis.com/v1/organizations/my-organization/environments/test/resourcefiles/properties/MyPropSet" \
  -H "Authorization: Bearer $TOKEN"
  

$TOKEN is set to your OAuth 2.0 access token, as described in Obtaining an OAuth 2.0 access token. For information about the curl options used in this example, see Using curl.

The following provides an example of the response.

# myProps.properties file
# General properties
foo=bar
baz=biff

# Messages/notes/warnings
message=This is a basic message.
note_message=This is an important message.
error_message=This is an error message.

# Keys
publickey=abc123
privatekey=splitwithsoundman

For more information, see List environment resource files API.

Update property sets using the API

The following sections describe how to update property sets scoped to an API proxy revision or environment using the API.

Update property sets scoped to an API proxy revision using the API

To update a property set that is scoped to an API proxy revision using the API:

  1. Update the property set file.
  2. Download the API proxy configuration bundle using the Get API proxy revision API with the following options:
    • Set the format query parameter to bundle
    • Set the Accept header to application/zip
  3. Add the property set file to the API proxy configuration bundle.
  4. Upload the API proxy configuration bundle using the Update API proxy revision API.

Update property sets scoped to an environment using the API

To update a property set that is scoped to an environment using the API, update the property set file and then upload it to an environment in your organization by issuing a PUT request to the following resource: https://apigee.googleapis.com/v1/organizations/{org}/environments/{env}/resourcefiles/{type}/{name}

Include the following information with your request:

  • Set {type} to properties
  • Set {name} to the name of the property set that you want to update
  • Pass the contents of the property set file as application/octet-stream or multipart/form-data

The following example updates the MyPropSet property set using the contents of the /Users/myhome/myprops-revised.properties file:

curl -X PUT "https://apigee.googleapis.com/v1/organizations/my-organization/environments/test/resourcefiles/properties/MyPropSet"
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-type: multipart/form-data" \
  -F file=@/Users/myhome/myprops-revised.properties
  

$TOKEN is set to your OAuth 2.0 access token, as described in Obtaining an OAuth 2.0 access token. For information about the curl options used in this example, see Using curl.

The following provides an example of the response.

{
  "name": "MyPropSet",
  "type": "properties"
}

For more information, see Update resource file API.

Delete property sets using the API

The following sections describe how to delete property sets scoped to an API proxy revision or environment using the API.

Delete property sets scoped to an API proxy revision using the API

To delete a property set that is scoped to an API proxy revision using the API:

  1. Download the API proxy configuration bundle using the Get API proxy revision API with the following options:
    • Set the format query parameter to bundle
    • Set the Accept header to application/zip
  2. Delete the property set file from the API proxy configuration bundle.
  3. Upload the API proxy configuration bundle using the Update API proxy revision API.

Delete property sets scoped to an environment using the API

To delete a property set scoped to an environment using the API, issue a DELETE request to the following resource: https://apigee.googleapis.com/v1/organizations/{org}/environments/{env}/resourcefiles/{type}/{name}

Pass the following information with your request:

  • Set {type} to properties
  • Set {name} to the name of the property set that you want to delete

The following example deletes the MyPropSet property set:

curl -X DELETE https://apigee.googleapis.com/v1/organizations/my-organization/environments/test/resourcefiles/properties/MyPropSet \
  -H "Authorization: Bearer $TOKEN"
  

$TOKEN is set to your OAuth 2.0 access token, as described in Obtaining an OAuth 2.0 access token. For information about the curl options used in this example, see Using curl.

The following provides an example of the response.

{
  "name": "MyPropSet",
  "type": "properties"
}

For more information, see Delete resource file API.

Access property set values

Access property set values anywhere in an API proxy where you can access flow variables: in policies, flows, JavaScript code, and so on.

To access values in a property set, use the following syntax:

propertyset.[property_set_name].[property_name]

Where:

  • property_set_name is the filename that you defined (if you used the UI) or the value of the name query parameter (if you used the API).
  • property_name is the name of the property. For example, if your property set contains foo=bar, foo is the name of the property and bar is the value.

For example, in a JavaScript policy, use the getVariable() method to get a value from a property set:

context.getVariable('propertyset.property_set_name.property_name);

The following example gets the value of the foo property in the property set named "MyPropSet":

context.getVariable('propertyset.MyPropSet.foo);

You can also use the ExtractVariables policy to get the value of a property from a property set and assign it to another variable, as the following example shows:

<ExtractVariables name="ExtractVariables-1">
   <DisplayName>Extract a portion of the url path</DisplayName>
   <Source>request</Source>
   <Variable name="propertyset.MyPropSet.foo">
      <Pattern>{myVar}</Pattern>
   </Variable>
   <VariablePrefix>foobar</VariablePrefix>
   <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</ExtractVariables>

In this example, the policy reads the variable from the attribute name, and assigns the value to the variable in the Pattern element.

Assign the value of a property set key dynamically using the Assign Message policy

You can use the Assign Message policy to assign the value of property set key to a flow variable dynamically. For details, see the PropertySetRef description in the Assign Message policy reference.

For Apigee hybrid users

If you are using Apigee hybrid, note the following:

  1. The hybrid management plane validates the property set; if valid, the properties are stored in the management plane.
  2. The Synchronizer retrieves the property set data and stores it locally on the runtime plane.
  3. The Message Processor loads the downloaded property set and makes it available to executing proxies.