这篇文章已经过机器翻译.放弃
参数-默认源代码构造
可以使用此构造来重用其他样书中的参数定义。
假设这样一个场景:一个参数或一组参数在多个样本中重复使用。为了避免重新定义这些参数,每次要创建新样书时,可以将其定义一次,然后使用 parameters-default-sources 构造将其定义导入需要这些参数的样书。
例如,如果多个样书需要配置虚拟 IP,可能必须在创建的每个新样书中定义与虚拟 IP 有关的相同参数。但是可以创建一个单独的样书(例如,名为“vip-params”),在其中定义与其有关的所有参数,如以下示例中所示:
- name: vip-params namespace: com.acme.commontypes version: "1.0" description: This StyleBook defines a typical virtual IP config. private: true schema-version: "1.0" parameters: - name: lb-appname label: Load Balanced Application Name description: Name of the Load Balanced application type: string required: true - name: lb-virtual-ip label: Load Balanced App Virtual IP address description: Virtual IP address representing the Load Balanced application type: ipaddress required: true - name: lb-virtual-port label: Load Balanced App Virtual Port description: TCP port representing the Load Balanced application type: tcp-port default: 80 - name: lb-service-type label: Load Balanced App Protocol description: Protocol used for the Load Balanced application. type: string default: HTTP required: true allowed-values: - HTTP - SSL - TCP
之后可以创建利用这些参数的其他样书。下面是这样一个样本示例。
- name: acme-biz-app namespace: com.acme.stylebooks version: "1.0" description: This stylebook defines the NetScaler configuration for Biz App schema-version: "1.0" import-stylebooks: - namespace: com.acme.commontypes prefix: cmtypes version: "1.0" parameters-default-sources: - cmtypes::vip-params parameters: - name: monitorname label: Monitor Name description: Name of the monitor type: string required: true - name: type label: Monitor Type description: Type of the monitor type: string required: true allowed-values: - PING - TCP - HTTP - HTTP-ECV - TCP-ECV - HTTP-INLINE
在 acme-biz-app 样书中,首先通过使用“import-样书”部分导入 vip-params 样书的命名空间和版本。然后添加 parameters-default-sources 构造,并指定样书名称,即 vip-params。这与在此样本中直接定义 vip-params 样本的参数效果一样。
由于 parameters-default-sources 是一个列表,且列表中的每个项目都需要是一个样书,因此可以包括多个样书中的参数。
除了包括其他样本中的参数外,还可以使用 parameters 部分定义您自己的参数。样本的完整参数列表是从其他样本中添加的参数和此样本中定义的参数的组合。因此,表达式 $parameters 是指此参数组合。
请注意,如果一个参数在导入的样本和当前样本中都定义了,则当前样本中的定义覆盖从其他样本导入的定义。可以在需要时自定义一些导入参数,同时按原样使用其余导入参数,有效利用这一点。
parameters-default-sources 构造还可以用于嵌套的参数中,如下所示:
parameters: - name: vip-details label: Virtual IP details description: Details of the Virtual IP type: object required: true parameters-default-sources: - cmtypes::vip-params
这与在此样书中将 vip-params 样书的参数直接作为 vip-details 参数的子参数添加类似。