update_config_xml ( $xml, $opt )

Updates an existing configuration XML document.

Access

public

Parameters

Parameter

Type

Required

Description

$xml

CFSimpleXML
CFResponse
string

Required

The source configuration XML to make updates to. Can be the CFSimpleXML body of a get_distribution_config() response, the entire CFResponse of a get_distribution_config() response, or a string of XML generated by generate_config_xml() or update_config_xml().

$opt

array

Optional

An associative array of parameters that can have the following keys:

  • CNAME - string|array - Optional - The value or values to add to the existing list of CNAME values. If setting more than one, use an indexed array. Supports up to 10 CNAMEs.
  • Comment - string - Optional - A comment to apply to the distribution. Cannot exceed 128 characters.
  • DefaultRootObject - string - Optional - The file to load when someone accesses the root of your Amazon CloudFront domain (e.g., index.html).
  • Enabled - string - Optional - A value of true enables the distribution. A value of false disables it. The default value is true.
  • Logging - array - Optional - An array that contains two keys: Bucket, specifying where logs are written to, and Prefix, specifying a prefix to append to log file names.
  • OriginAccessIdentity - string - Optional - The origin access identity (OAI) associated with this distribution. Use the Identity ID from the OAI, not the CanonicalId.
  • TrustedSigners - array - Optional - An array of AWS account numbers for users who are trusted signers. Explicity add the value Self to the array to add your own account as a trusted signer.

Returns

Type

Description

string

XML document.

Examples

Update a distribution with comments, a single CNAME, and a default root object.

$cdn = new AmazonCloudFront();

$config_xml = $cdn->get_distribution_config('E2L6A3OZHQT5W4');

// Update the XML content
$response = $cdn->update_config_xml($config_xml, array(
	'Enabled' => true,
	'Comment' => 'This is my sample comment',
	'CNAME' => 'cname.example.com',
	'DefaultRootObject' => 'text/sample.txt'
));

// Success?
var_dump($response);

Update a streaming distribution with comments, a single CNAME, and a default root object.

$cdn = new AmazonCloudFront();

$config_xml = $cdn->get_distribution_config('E2L6A3OZHQT5W4');

// Update the XML content
$response = $cdn->update_config_xml($config_xml, array(
	'Enabled' => true,
	'Comment' => 'This is my sample comment',
	'CNAME' => 'cname.example.com',
	'Streaming' => true,
	'DefaultRootObject' => 'text/sample.txt'
));

// Success?
var_dump($response);

Related Methods

Source

Method defined in services/cloudfront.class.php | Toggle source view (212 lines) | View on GitHub

public function update_config_xml($xml, $opt = null)
{
    // If we receive a full CFResponse object, only use the body.
    if ($xml instanceof CFResponse)
    {
        $xml = $xml->body;
    }

    // If we received a string of XML, convert it into a CFSimpleXML object.
    if (is_string($xml))
    {
        $xml = simplexml_load_string($xml, $this->parser_class);
    }

    // Default, empty XML
    $update = simplexml_load_string(sprintf($this->base_xml, (
        (isset($opt['Streaming']) && $opt['Streaming'] == (bool) true) ? 'StreamingDistributionConfig' : 'DistributionConfig')
    ), $this->parser_class);

    // These can't change.
    if (isset($xml->S3Origin))
    {
        $origin = $update->addChild('S3Origin');
        $origin->addChild('DNSName', $xml->S3Origin->DNSName);

        // origin access identity
        if (isset($opt['OriginAccessIdentity']))
        {
            $origin->addChild('OriginAccessIdentity', 'origin-access-identity/cloudfront/' . $opt['OriginAccessIdentity']);
        }
        elseif (isset($xml->S3Origin->OriginAccessIdentity))
        {
            $origin->addChild('OriginAccessIdentity', $xml->S3Origin->OriginAccessIdentity);
        }
    }
    elseif (isset($xml->CustomOrigin))
    {
        $origin = $update->addChild('CustomOrigin');
        $origin->addChild('DNSName', $xml->CustomOrigin->DNSName);

        // Copy OriginProtocolPolicy for update
        if ( isset($xml->CustomOrigin->OriginProtocolPolicy) )
        {
            $origin->addChild('OriginProtocolPolicy', $xml->CustomOrigin->OriginProtocolPolicy);
        }

        // origin access identity
        if (isset($opt['OriginAccessIdentity']))
        {
            $origin->addChild('OriginAccessIdentity', 'origin-access-identity/cloudfront/' . $opt['OriginAccessIdentity']);
        }
        elseif (isset($xml->CustomOrigin->OriginAccessIdentity))
        {
            $origin->addChild('OriginAccessIdentity', $xml->CustomOrigin->OriginAccessIdentity);
        }
    }

    $update->addChild('CallerReference', $xml->CallerReference);

    // Add existing CNAME values
    if ($xml->CNAME)
    {
        $update->addChild('CNAME', $xml->CNAME);
    }

    // Add new CNAME values
    if (isset($opt['CNAME']))
    {
        if (is_array($opt['CNAME']))
        {
            foreach ($opt['CNAME'] as $cname)
            {
                $update->addChild('CNAME', $cname);
            }
        }
        else
        {
            $update->addChild('CNAME', $opt['CNAME']);
        }
    }

    // Comment
    if (isset($opt['Comment']))
    {
        $update->addChild('Comment', $opt['Comment']);
    }
    elseif (isset($xml->Comment))
    {
        $update->addChild('Comment', $xml->Comment);
    }

    // DefaultRootObject
    if (isset($opt['DefaultRootObject']))
    {
        $update->addChild('DefaultRootObject', $opt['DefaultRootObject']);
    }
    elseif (isset($xml->DefaultRootObject))
    {
        $update->addChild('DefaultRootObject', $xml->DefaultRootObject);
    }

    // Enabled
    if (isset($opt['Enabled']))
    {
        $update->addChild('Enabled', $opt['Enabled'] ? 'true' : 'false');
    }
    elseif (isset($xml->Enabled))
    {
        $update->addChild('Enabled', $xml->Enabled);
    }

    // Logging
    if (isset($opt['Logging']))
    {
        if (is_array($opt['Logging']))
        {
            $logging = $update->addChild('Logging');
            $bucket_name = $opt['Logging']['Bucket'];

            // Origin
            $logging->addChild('Bucket', $bucket_name . ((stripos($bucket_name, '.s3.amazonaws.com') === false) ? '.s3.amazonaws.com' : ''));

            $logging->addChild('Prefix', $opt['Logging']['Prefix']);
        }
    }
    elseif (isset($xml->Logging))
    {
        $logging = $update->addChild('Logging');
        $logging->addChild('Bucket', $xml->Logging->Bucket);
        $logging->addChild('Prefix', $xml->Logging->Prefix);
    }

    // Required Protocols
    if (isset($opt['RequiredProtocols']))
    {
        $required_protocols = $xml->addChild('RequiredProtocols');
        $required_protocols->addChild('Protocol', $opt['RequiredProtocols']);
    }
    elseif (isset($xml->RequiredProtocols))
    {
        $required_protocols = $update->addChild('RequiredProtocols');
        $required_protocols->addChild('Protocol', $xml->RequiredProtocols->Protocol);
    }

    // Caching Behavior
    if (isset($opt['CachingBehavior']) && is_array($opt['CachingBehavior']))
    {
        $caching_behavior = $xml->addChild('CachingBehavior');

        if (isset($opt['CachingBehavior']['MinTTL']))
        {
            $min_ttl = $opt['CachingBehavior']['MinTTL'];

            if (is_string($min_ttl))
            {
                $min_ttl = strtotime($min_ttl);
            }

            $caching_behavior->addChild('MinTTL', $min_ttl);
        }
    }
    elseif (isset($xml->CachingBehavior))
    {
        $caching_behavior = $update->addChild('CachingBehavior');
        $caching_behavior->addChild('MinTTL', $xml->CachingBehavior->MinTTL);
    }

    // Trusted Signers
    if (isset($opt['TrustedSigners']))
    {
        $trusted_signers = $update->addChild('TrustedSigners');

        // Not an array? Convert to one.
        if (!is_array($opt['TrustedSigners']))
        {
            $opt['TrustedSigners'] = array($opt['TrustedSigners']);
        }

        // Handle 'Self' vs. everything else
        foreach ($opt['TrustedSigners'] as $signer)
        {
            if (strtolower($signer) === 'self')
            {
                $trusted_signers->addChild('Self');
            }
            else
            {
                $trusted_signers->addChild('AwsAccountNumber', $signer);
            }
        }
    }
    elseif (isset($xml->TrustedSigners) && $xml->TrustedSigners->count())
    {
        $trusted_signers = $update->addChild('TrustedSigners');

        // Handle 'Self' vs. everything else
        foreach ($xml->TrustedSigners->children() as $signer_key => $signer_value)
        {
            if (strtolower((string) $signer_key) === 'self')
            {
                $trusted_signers->addChild('Self');
            }
            else
            {
                $trusted_signers->addChild('AwsAccountNumber', (string) $signer_value);
            }
        }
    }

    // Output
    return $update->asXML();
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback